Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
819 views
in Technique[技术] by (71.8m points)

javascript - Override window.open()

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);
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...