覆盖window.open() [英] Override window.open()
问题描述
我正在尝试覆盖 window.open()
,所以我试图在没有的情况下找到在新窗口中打开链接的方法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()
.
详细说明,我遇到这种情况:我有一个使用<的Silverlight Web部件code> HTMLWindow.Navigate()在新窗口中打开超链接,因此我无法使用< a>
因为我有完全无法控制Web部分。唯一的方法是覆盖 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()
.
例如,当我想打开链接时在顶部窗口,我覆盖 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:我不能在这里使用锚点因为我重写 window.open()
PS: I cannot use anchor here because I'm overriding window.open()
我最后为这个问题做了什么:
首先,我做了一个的副本window.open()
var _open = window.open;
然后我重写 window.open()
功能。例如,假设target是接收查询字符串的变量。请注意,默认行为是在当前窗口中打开链接。
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);
}
推荐答案
仅从javascript即可' t使用 window.open()
打开一个新窗口,不带。此外,您不会影响在新浏览器中打开窗口的方式。这取决于浏览器设置是否会打开新选项卡或新窗口。
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.
您可以通过定义来声明您想要一个新窗口< a
目标= _空白
强> HREF = ... >< / A>
在 a
-element上,但用户必须单击它。否则,如果您使用javascript触发单击,则会遇到浏览器的弹出窗口阻止程序。
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.
这篇关于覆盖window.open()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!