关于打开/弹出新窗口的浏览器警报
我有一个链接,单击该链接时,浏览器将打开一个新窗口。这是该链接上单击事件的代码:
var clickView = function(){
window.open('/client/item/show/' + itemID);
return false;
};
我还有另一个函数,可以从 ajax 调用中读取 url 并在新窗口中打开它。
if (json.data && json.data.URL)
{
if (me.urlTarget==='_self'){
//use the self window to open the URL
window.location.href =json.data.URL;
} else{
//use new window to open the url.
window.open(json.data.URL);
}
}
对于第一个功能(clickView),浏览器(IE7/8和Firefox)将打开一个新选项卡,而不向用户发出任何警告。对于第二个函数(从 json.data.URL 读取 url),IE 和 Firefox 都会显示警告消息并阻止新窗口,直到用户同意警告为止。在这两个函数中,打开 URL 是相同的。
我想知道为什么会有差异,是否有可能使它们的行为一致?
I have a link that when clicked, the browser will open a new window. This the code for the click event on that link:
var clickView = function(){
window.open('/client/item/show/' + itemID);
return false;
};
I also have another function that, read the url from a ajax call and open it in a new window.
if (json.data && json.data.URL)
{
if (me.urlTarget==='_self'){
//use the self window to open the URL
window.location.href =json.data.URL;
} else{
//use new window to open the url.
window.open(json.data.URL);
}
}
For the first function (clickView), the browser (IE7/8 & Firefox) will open a new tab without any warning to user. For the second function (where the url is read from json.data.URL), both IE and Firefox will show an warning message and block the new window until the user agree on the warning. In both functions the opening URL is the same.
I'm wondering why is there a difference, and is it possible to make them behave consistent?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
答案似乎在这里:
在浏览器不发出警告的情况下打开新窗口这是一个弹出窗口
摘要:在看似随机的时间调用 window.open() 会导致浏览器启动并发出警告/提示。单击链接后调用 window.open() 效果很好。
The answer appears to be here:
open new window without the browser giving warning that is a popup
Summary: calling window.open() at seemingly random times causes the browser to kick in with a warning/prompt. Calling window.open() as a result of a link click works fine.
可能第二个实例会生成警告,因为它是绝对 URL,而不是相对 URL?
(无论哪种方式,打开新的浏览器窗口都是魔鬼的工作)。
Possibly the second instance generates a warning because it is an absolute, rather than relative URL?
(Either way, opening new browser windows is the devil's work).