javascript jQuery window.open 不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25764960/
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-10-28 04:58:26  来源:igfitidea点击:

jQuery window.open not working

javascriptjqueryajax

提问by user2363971

I have very simple code

我有非常简单的代码

$('.opennewwindow').click(function(e) {
    e.preventDefault();
    window.open("http://www.stackoverflow.com")
    /*alert('its works');*/
});

<button class="opennewwindow" value="somevalue">Open new window!</button>

Unfortunately, the code does not work (the function calls, but not open.window). Errors dont appear anywya.

不幸的是,代码不起作用(函数调用,但不是 open.window)。无论如何都不会出现错误。

I tried like this too:

我也这样试过:

$('.opennewwindow').click(function(e) {
    e.preventDefault();
    myWin = window.open("http://www.stackoverflow.com");
    myWin.focus();
});

and following error TypeError: myWin is undefined.

和以下错误TypeError: myWin is undefined

I used a $(document).ready()etc. but same effect.

我使用了$(document).ready()等,但效果相同。

The clue is that on my website I have a ajax scripts. This button and function is also loaded by ajax.

线索是在我的网站上我有一个 ajax 脚本。这个按钮和功能也是由ajax加载的。

Anyone know why this is not working?

有谁知道为什么这不起作用?

回答by TeeDeJee

Your first code just works. See http://jsfiddle.net/69a1cL1v/

您的第一个代码可以正常工作。见http://jsfiddle.net/69a1cL1v/

It is probably a pop-up blocker.

这可能是一个弹出窗口阻止程序。

$('.opennewwindow').click(function(e) {
    e.preventDefault();
    window.open("http://www.stackoverflow.com")
    /*alert('its works');*/
});

回答by user2363971

Oh man what I done!

哦,伙计,我做了什么!

I just (in my custom jquery scripts) override function "open"... I dont know why I did it (it was unused function). I'm sorry for the trouble.

我只是(在我的自定义 jquery 脚本中)覆盖函数“打开”......我不知道我为什么这样做(它是未使用的函数)。我很抱歉给您带来麻烦。

回答by PythonDev

Tools > Options, Content area, "Advanced" button to the right of "Enable JavaScript" should give you the option to allow this behavior. If you're on Linux, the navigation might be slightly different.

工具 > 选项、内容区域、“启用 JavaScript”右侧的“高级”按钮应该为您提供允许此行为的选项。如果您使用的是 Linux,导航可能会略有不同。

Note, it is generally considered best practice to allow the user to choose what window should be in focus.

请注意,允许用户选择应该聚焦的窗口通常被认为是最佳实践。

This is about:config you're referring to: dom.disable_window_flip

这是 about:config 你指的是:dom.disable_window_flip

In FF I am able to open a new window with focus by providing window name and option parameters to window.open, and then calling focus.

在 FF 中,我可以通过向 window.open 提供窗口名称和选项参数,然后调用焦点来打开一个具有焦点的新窗口。

var wi = window.open('http://www.stackoverflow.com', 'window_name', 'height=200,width=200');
wi.focus();

For Chrome support, you have to check out: Google Chrome "window.open" workaround?

对于 Chrome 支持,您必须查看:Google Chrome“window.open”解决方法?