javascript - window.open 不会在同一窗口中再次打开

标签 javascript jquery

每次重新加载特定页面时,都会打开一个弹出窗口,以便通过谷歌搜索第二个 .问题是它一直在新窗口中打开,而不是在原始弹出窗口中打开。

var td_name = $("td:eq(1)");
var td_text = td_name.text();
window.open("http://www.google.com/search?q="+td_text+"", "myWindow", '_blank');

最佳答案

浏览器安全模型已经阻止了这种情况一段时间(可以理解)。如果“重新加载”是用 JS 完成的,而不是完全浏览器刷新,它将起作用。

请参阅此相关答案以获取使用窗口引用的解决方法:

No. Without a reference to the window, you can't find it again, by name or otherwise. There is no collection of windows.

UPDATE: Here's how you could do it yourself:

var windows = {};
function openWindow(url, name, features) {
    windows[name] = window.open(url, name, features);
    return windows[name];
}

Now, openWindow will always open the window, and if the window already exists, it will load the given URL in that window and return a reference to that window. Now you can also implement findWindow:

function findWindow(name) {
    return windows[name];
}

Which will return the window if it exists, or undefined.

You should also have closeWindow, so you don't keep references to windows that you opened yourself:

function closeWindow(name) {
    var window = windows[name];
    if(window) {
        window.close();
        delete windows[name];
    }
}

If it is not possible, what is the point giving windows names?

该名称由浏览器内部使用来管理窗口。如果你 调用window.open使用相同的名称,它不会打开新窗口,但 而是将 URL 加载到之前打开的窗口中。有一个 还有一些事情,来自 MDN window.open() :

If a window with the name strWindowName already exists, then strUrl is loaded into the existing window. In this case the return value of the method is the existing window and strWindowFeatures is ignored. Providing an empty string for strUrl is a way to get a reference to an open window by its name without changing the window's location. To open a new window on every call of window.open(), use the special value _blank for strWindowName.

来源: https://stackoverflow.com/a/9364899

关于javascript - window.open 不会在同一窗口中再次打开,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31277226/

相关文章:

javascript - Javascript 中的函数声明

jquery - 我的状态改变了,但页面没有正确刷新

javascript - 向下滚动显示标题并在向上滚动时隐藏

javascript - 我们如何使用 Touch 事件在 div 中滚动元素

javascript - iframe 破坏了 jquery.contents?

javascript - Ember 依赖项 "Uncaught TypeError: $(...).fitText is not a function"

javascript - Google 不是使用 Google Visualization API 定义的;可能是 jQuery 的错

javascript - 无需 react 即可访问 redux

javascript - jQuery 隐藏功能的问题

javascript - Google map API 找不到 $(document).ready 中定义的回调