简体   繁体   English

如何使用JavaScript从iframe打开链接

[英]How to open link from iframe using javascript

I have this: 我有这个:

<td onclick="window.location='http://www.google.com/';"> </td>

The problem is that I have this inside an iframe, so onclick it opens in the iframe. 问题是我将其放置在iframe中,因此onclick在iframe中将其打开。 How do I open this link on the parent page instead of the iframe? 如何在父页面而不是iframe上打开此链接?

(Is there a way to do this with one js script for all the links on the page?) (是否可以使用一个js脚本针对页面上的所有链接执行此操作?)

window.parent.location.href='http://www.google.com/';

如果要重新加载最上面的窗口:

window.top.location.href='http://www.google.com/';

one way to do this with on js script for all the links on the page is to add onClick event on the links when the page is loaded. 在页面上所有链接上使用js脚本执行此操作的一种方法是在加载页面时在链接上添加onClick事件。 here is a quick code. 这是一个快速代码。 put this code in a file and put it in an iframe. 将此代码放在文件中,然后放在iframe中。

<html>
<script>
function openInParent() {
  window.parent.location = this.href;
  return false;
}

function doload () {
  // loop through all links in the page to add onclick event
  var links = document.getElementsByTagName ('a');
  for(i in links) {
    links[i].onclick = openInParent;
  }
}

window.onload = doload;
</script>
<body>
<a href="http://www.google.com">test</a>
</body>
</html>

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

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