Javascript window.open 在 IE 中不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3662531/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 05:37:54  来源:igfitidea点击:

window.open not working in IE

javascriptjqueryhtmlinternet-explorerwindow.open

提问by serv-bot 22

Apparently, this call to window.openis not valid under Internet Explorer. The Javascript code on my site is not running, I would assume it is due to that error.

显然,此调用window.open在 Internet Explorer 下无效。我网站上的 Javascript 代码没有运行,我认为这是由于该错误造成的。

The line it tells me the error is on, is the call to window.open, apparently an argument is not valid there.

它告诉我错误所在的行是对 window.open 的调用,显然参数在那里无效。

$('.objeto').click( 
        function() {
            var center   = 'height=380,width=900,top='+((screen.width - 900)/2)+',left='+((screen.height - 380)/2);
            var address = $(this).attr('id');
            window.open (address,'Ver articulo', config=center); 
        }
    );

The site runs fine under both Google Chrome, and Firefox.

该网站在 Google Chrome 和 Firefox 下运行良好。

回答by Dan Dumitru

In IE, you can't have spaces in your second variable (the new window's name).

在 IE 中,您的第二个变量(新窗口的名称)中不能有空格。

Try:

尝试:

window.open (address,'Ver_articulo', config=center); 

回答by Rillus

Also worth re-iterating that IE9 (and possibly below) doesn't like hyphens ('-') in the window name (2nd parameter).

同样值得重申的是,IE9(可能还有以下)不喜欢窗口名称(第二个参数)中的连字符('-')。

I know one of the comments mentioned this, but it's a bit buried - and it's one tip that just solved an issue for me.

我知道其中一个评论提到了这一点,但它有点被掩盖了 - 这是一个刚刚为我解决了一个问题的提示。

回答by Nick Craver

I'm not sure what config is, you just need:

我不确定什么是配置,你只需要:

window.open (address,'VerArticulo', center);

Keep in mind though, it looks like your idattribute is invalid to get the effect here, you probably want to use something different, e.g. data-href="urlHere"on the element, if it's not an anchor already.

但是请记住,看起来您的id属性在这里无法获得效果,您可能想要使用不同的东西,例如data-href="urlHere"在元素上,如果它已经不是一个锚点。

回答by John

even thou it's kind a late with answer for OP, but for someone else stumbling across this post it might help:

即使你对 OP 的回答有点晚,但对于其他人偶然发现这篇文章可能会有所帮助:

Had exactly same problem as OP after trying to use "window.open" method. It turns out that Chrome is ok with original href tag with URL in it where IE seems to get confused with that. After removing href from link worked spot on.

尝试使用“window.open”方法后遇到与 OP 完全相同的问题。事实证明,Chrome 可以使用带有 URL 的原始 href 标签,而 IE 似乎对此感到困惑。从链接中删除 href 后,就可以工作了。

CODE SAMPLE:

代码示例:

$(document).ready(function () 
{
    $('a[rel^="external"]').each(function () 
    {
        var externalLink = $(this);
        var externalLinkValue = externalLink.attr("href");
        externalLink.unbind('click');
        externalLink.removeAttr("href");

        externalLink.click(function (event)
        {
            event.preventDefault();
            followExtrenalLink = window.open(externalLinkValue,'_blank');
        });

        externalLink.hover(function ()
        {
            externalLink.css('cursor', 'pointer');
        });

    });