简体   繁体   English

如何在没有目标属性的选项卡式或新窗口中打开链接?

[英]How to make a link open in tabbed or new window without target attribute?

In my application an external website places link in my web page and the link does not have target=_blank. 在我的应用程序中,一个外部网站将链接放置在我的网页中,并且该链接没有target = _blank。 More specifically its iframe in my webpage whose src is another domain which I have no control. 更具体地说,它是我网页中的iframe,它的src是我无法控制的另一个域。 The content in this iframe contains links that opens in the same window wiping off my ajax driven web page and taking the user to the other domain. 此iframe中的内容包含在同一窗口中打开的链接,这些链接清除了我的Ajax驱动的网页并将用户带到另一个域。 Is there a way to intercept this ( via javascript? ) and make the link open in another window or browser tab? 有没有一种方法可以拦截(通过javascript?)并使链接在另一个窗口或浏览器选项卡中打开? It would be even much better if the link opens within the same iframe on my webpage. 如果链接在我网页上的同一iframe中打开,那就更好了。 Is this possible? 这可能吗? Is target="_self" makes the link open in the same iframe? 是target =“ _ self”使链接在同一iframe中打开吗?

You could add the target-attribute to all a-tags in the iframe. 您可以将目标属性添加到iframe中的所有a标签。 This can be done in jQuery, like: 这可以在jQuery中完成,例如:

$("a", ".myIframe").attr("target", "_blank");

This is just pseudo-code. 这只是伪代码。 See here, how to access elements in an iframe: 请参阅此处,如何访问iframe中的元素:

jQuery/JavaScript: accessing contents of an iframe jQuery / JavaScript:访问iframe的内容

** To open an url in a new tab with target attribute **要在具有目标属性的新标签页中打开网址

<a target="_blank" href="http://www.google.com" title="">
Click here for new tab
</a>

** To open an url in a new tab without target attribute. **要在没有目标属性的新标签页中打开网址。

<a onclick="window.open(this.href,'')" 
href="http://www.google.com"  title="">
Click here for new tab withouth target attribute</a>

** To open an url in the same frame. **在同一框架中打开一个URL。

<a target="_self" href="https://jsfiddle.net/" title="">
Click here to open in the same frame</a>

Please find the jsfiddle it works fine 请找到能正常工作的jsfiddle
jsfiddle jsfiddle

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM