繁体   English   中英

Javascript Onclick window.open Popup在Firefox和Safari中不起作用

[英]Javascript Onclick window.open Popup not working in Firefox and Safari

我的网站上 (密码是“ WS”,没有引号),我创建了一个带有插件的网格(UberGrid)。

为了使每个单元格弹出,我在此WordPress页面内添加了以下代码:

<script type="text/javascript">
function popWin(url)
{
    var thePopCode = window.open(url,'','height=800, width=1000, top=500, left=200, scrollable=yes, menubar=yes, resizable=yes');
    if (window.focus) 
    {
        thePopCode.focus();
    }
}
</script>

在插件内部(在Debra Porter单元内部),我添加了以下链接:

javascript: onclick=popWin('http://www.weybridgestrategies.com/team/debra-porter-president-ceo'); return (false);

它在Google Chrome浏览器中工作正常,但在Firefox或Safari中没有结果。

看一下产生的HTML:

<a class="uber-grid-hover" href="onclick=popWin(&#039;http://www.weybridgestrategies.com/team/debra-porter-president-ceo&#039;); return (false);"  >

它应该是什么样的:

<a class="uber-grid-hover" href="http://www.weybridgestrategies.com/team/debra-porter-president-ceo"
   onclick="popWin('http://www.weybridgestrategies.com/team/debra-porter-president-ceo'); return false;">

因此,您的popWin函数已经可以,但是您需要证明锚点的属性hrefonclick合理。 onclick是javaScript,因此不需要javaScript前缀,也不需要内联onclick=因为这会创建一个全局变量。 如果提供了JavaScript,则return false将阻止浏览器遵循href。 通过使用this.href它至少不会在IE中达到您的期望,因为这是IE中的事件,而不是锚点。

编辑:实际上,从Firefox Aurora v24开始,您的TestLink可以实现您的预​​期,而不会弹出窗口。

但是我必须遵循Brian的评论,您的新窗口可能会被视为弹出窗口,因此如果您执行window.open(url, '_blank')或仅使用<a target="_blank" href="..."> -并寻找一个不会加载新页面但看起来更像HTML5ish的javaScript“弹出窗口”,例如使用jQuery UI或尝试使用自己的jS(这将是更大的另一个答案。题... ;))

更新:释放已经包含的jQuery的一个好主意,让我们讲一下JavaScript:

<script type="text/javascript">
function popWin(url)
{
  var thePopCode = window.open(url,'','height=800, width=1000, top=500, left=200,scrollable=yes, menubar=yes, resizable=yes');
  if (window.focus) {
    thePopCode.focus();
  }
}

jQuery(document).ready(function ($) {
    // here is your HTML DOM ready

    // create an onclick event for every a.uber-grid-hover
    $("a.uber-grid-hover").click(function (event) {

        event.preventDefault(); // this is "like" return false

        // this get's the href from your anchor, using jQuery sugar
        var url = $(this).attr("href");

        // call your fun
        popWin(url);

    });

});

</script>

使用此脚本,您不再需要为每个锚点创建onclick属性。 只需将其放入您的wordpress来源中,就可以按原样工作。

现在,只需使用class="uber-grid-hover"来创建<a> ,这是必需的,以便jQuery可以轻松选择悬停,然后您需要href ,还可以包含target="_blank"以便使用非JavaScript用户还将在新窗口中显示该页面。

<a class="uber-grid-hover" target="_blank"
   href="http://www.weybridgestrategies.com/team/debra-porter-president-ceo">

试试这个代码:

function popwin(url) {
window.open('', url, 'height=800, width=1000, top=500, left=200, scrollable=yes, menubar=yes, resizable=yes');
url.target =url;
}

对于链接,请使用相同的代码

javascript: onclick=popWin('http://www.weybridgestrategies.com/team/debra-porter-president-ceo'); return (false);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM