简体   繁体   English

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

[英]window.open is not opening again in the same window

Every time a specific page reloads, a popup window is opened to a google search of what is contained in the 2nd . 每次重新加载特定页面时,都会打开一个弹出窗口,供Google搜索2nd中包含的内容。 The issue is that it keeps opening in a new window instead of inside the original popup. 问题在于,它一直在新窗口中打开,而不是在原始弹出窗口中打开。

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

The browser security model has prevented that for a while (understandably). 浏览器安全模型阻止了一段时间(可以理解)。 It will work if the "reload" is done with JS and not a full browser refresh. 如果使用JS完成“重新加载”,而不是完全刷新浏览器,则它将起作用。

See this related answer for a workaround using window references: 有关使用窗口引用的解决方法,请参见以下相关答案:

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. 现在, openWindow将始终打开该窗口,如果该窗口已经存在,它将在该窗口中加载给定的URL并返回对该窗口的引用。 Now you can also implement findWindow : 现在,您还可以实现findWindow

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

Which will return the window if it exists, or undefined . 如果存在,则返回窗口,或者返回undefined

You should also have closeWindow , so you don't keep references to windows that you opened yourself: 您还应该具有closeWindow ,因此不要保留对自己打开的窗口的引用:

 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? 如果不可能的话,给Windows命名的意义是什么?

The name is used internally by the browser to manage windows. 该名称在浏览器内部用于管理窗口。 If you call window.open with the same name, it won't open a new window but instead load the URL into the previously opened window. 如果您使用相同的名称调用window.open ,则不会打开新窗口,而是将URL加载到先前打开的窗口中。 There are a few more things, from MDN window.open() : MDN window.open()中还有更多内容:

If a window with the name strWindowName already exists, then strUrl is loaded into the existing window. 如果已经存在名称为strWindowName的窗口,则将strUrl加载到现有窗口中。 In this case the return value of the method is the existing window and strWindowFeatures is ignored. 在这种情况下,该方法的返回值是现有窗口,而strWindowFeatures被忽略。 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. 为strUrl提供空字符串是一种通过其名称获取对打开的窗口的引用而无需更改窗口位置的方法。 To open a new window on every call of window.open(), use the special value _blank for strWindowName. 要在每次调用window.open()时打开一个新窗口,请对strWindowName使用特殊值_blank。

SOURCE: https://stackoverflow.com/a/9364899 消息来源: https : //stackoverflow.com/a/9364899

try this 尝试这个

       function openWindow(td_text)
                {

                   var strurl ="http://www.google.com/search?q="+td_text;               

 window.open(strurl,"mywindow","menubar=0,resizable=1,scrollbars=1,width=550,height=200");
                }
        var td_name = $("td:eq(1)");
        var td_text = td_name.text();
        openWindow(td_text);

you can use 您可以使用

window.open(url,"_self");

then it will open in the same window. 然后它将在同一窗口中打开。

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

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