问题
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 <a
target="_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