将页面跳转到iframe

时间:2016-12-06 12:59:39

标签: javascript iframe page-jump

我有一个包含iframe的网页(http://www.WillenhallPrimary.org/Science.shtml)。 iframe包含<h2 id="staff">Teachers</h2>标记,我想使用父网址和哈希值跳转到该标记。 http://www.WillenhallPrimary.org/Science.shtml#staff。我也想等到页面加载才跳。 (我可以使用?而不是#,如果这更容易。)

使用onLoad:javascript调整iframe的大小(因此它看起来像一个连续的 这是有效的,但我不知道这是否会影响它。

 function rfs(){
   _i=document.getElementById('ym')
   _i.height=150
   if(typeof _i.contentDocument!=='undefined'){
     _cdd=_i.contentDocument.documentElement
     _i.height=Math.max(_cdd.scrollHeight,_cdd.offsetHeight,_cdd.clientHeight)+25
   }
   else {
     _cdb=_i.contentWindow.document.body
     _i.height=Math.max(_cdb.scrollHeight,_cdb.offsetHeight,_cdb.clientHeight)+25
   }
 }

这可能吗?这听起来很简单,但我花了几个小时看,没有任何工作。 (我不是这方面的专家。)有更好的方法吗?

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

@mplungjan感谢重定向 - 立即解决了它。

我最终得到的代码是:

window.onload=function(){
  if(window.location.hash!=''){
    document.getElementById('iframe_id').contentWindow.location.hash=window.location.hash
  }
}

似乎工作正常。非常高兴: - )