Override window.open()

对着背影说爱祢 提交于 2019-11-28 06:16:08

问题


I'm trying to override window.open(), so I'm trying to find a way to open link in new window without window.open().

In detail, I'm in such a situation: I've got a Silverlight Web Part Which uses HTMLWindow.Navigate() to open hyperlinks in new window, so I cannot use <a> because I've got totally no control to the Web part. The only way is to override window.open().

For example, when I want to open link in the top window, I override window.open() like this:

window.open = function (open) {
    return function (url, name, features) {
            window.top.location = url;    
            return false;
       };
    }(window.open);

Is there a similar method to open link in new window? Thank you very much!

PS: I cannot use anchor here because I'm overriding window.open()

What I did finally for this problem:

Firstly, I made a copy of window.open()

var _open = window.open;

Then I override the window.open() function. Supposing that "target" is a variable that receives querystring, for example. Note that the default behavior is to open the link in the current window.

if (target == "top")
    {
        window.open = function (open) {
            return function (url, name, features) {
                window.top.location = url;
                return false;
           };
        }(window.open);
    }
else if (target == "blank")
{
    window.open = function (open) {
            return function (url, name, features) {
                return open_(url, '_blank', features);
           };
        }(window.open);
}

回答1:


From javascript alone you can't open a new window without using window.open(). Also you have no influence on how the window is opened in new browsers. It depends on the browser-settings whether this opens a new tab or a new window.

You can declare that you want a new window by defining <atarget="_blank"href="..."></a> on a a-element, but then the user has to click it. Otherwise if you trigger a click with javascript you will run into the popup blockers of the browsers.



来源:https://stackoverflow.com/questions/14376522/override-window-open

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