Google Chrome Userscripts reference window.open

不想你离开。 提交于 2019-12-11 04:06:02

问题


I have been trying to figure this one out for a while, but when I try to reference a window that I opened the handle is always undefined.

It is worth noting that this is being used in a userscript and here is the snippet in question:

var donateWindow;

// ######################################################################
// # Show the donation popup and open a window to paypal site
// ######################################################################
function showDonateWindow()
{
    if (window.confirm("Question here"))
    {
      if (! (typeof(donateWindow) == 'undefined' || donateWindow.closed)) window.donateWindow.close();
      window.donateWindow = window.open("http://somesite.com/","tabName");
    }
}

Any help on this would be very appreciated. It would seem no matter what I do window.open returns the value "undefined".

My goal is to have a popup shown, but if one is already open it should just replaced the old one. This works as expected in FF, but for the life of me I can not get it going in Chrome.


回答1:


Why are you trying to close existing window before open a new one? you don't need to do that.

if you just use the same name for window when you open it, it will replace the existing one if there is.

this means you don't need to look for if there is an opened window.

function showDonateWindow()
{
    if (window.confirm("Question here"))
    {
        window.open("http://somesite.com/","donateWindowName");
    }
}


来源:https://stackoverflow.com/questions/10147623/google-chrome-userscripts-reference-window-open

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!