0

Every time a specific page reloads, a popup window is opened to a google search of what is contained in the 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');
Brandon Hellman
  • 103
  • 2
  • 8

3 Answers3

2

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.

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. 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?

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. There are a few more things, from 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.

SOURCE: https://stackoverflow.com/a/9364899

Community
  • 1
  • 1
Alain O'Dea
  • 21,033
  • 1
  • 58
  • 84
  • This seems to be gong way over my head for some reason. Would the correct format now be this? It is not working for me. `var windows = {}; function openWindow("http://www.google.com/search?q="+td_text+"", "myWindow", '_blank') { windows[myWindow] = window.open("http://www.google.com/search?q="+td_text+"", "myWindow", '_blank'); return windows[myWindow]; }` – Brandon Hellman Jul 07 '15 at 20:16
  • @BrandonHellman the answer I have cited there won't work around full page refreshes. There isn't a solution for that that I know of. I think it is intentionally impossible for security reasons. – Alain O'Dea Jul 07 '15 at 20:46
0

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);
kavetiraviteja
  • 2,058
  • 1
  • 15
  • 35
0

you can use

window.open(url,"_self");

then it will open in the same window.

Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
user11100340
  • 53
  • 1
  • 8