2010-04-19 40 views
5

我已经尝试了一些不同的东西,但没有真正的工作在浏览器的URL iframe和更新哈希获取URL,基本上我需要从iframe中获取当前位置/ URL,得到我想要的一部分,它返回哈希在网址中。我怎么能在JavaScript中做到这一点?

回答

6

选择正确的IFRAME元素,拉出SRC属性,做你的东西,转让src指向window.location.hash

var iframe = $('iframe'); 
var src = iframe.attr('src'); 
window.location.hash = src; 

编辑

如果你想获得动态从iframe你的位置访问contentWindow属性:

var iframe  = $('iframe'); 
var contentWnd = iframe.attr('contentWindow'); 
var url = contentWnd.window.location.href; 

window.location.hash = url; 

上获得contentWindow属性也值得一读:

http://www.bennadel.com/blog/1592-Getting-IFRAME-Window-And-Then-Document-References-With-contentWindow.htm

+3

当用户浏览/将不同页面的iframe中没有更新,SRC从而违背了目的。其他解决方案? – Adam 2010-04-19 22:47:04

+2

+1尼斯的答案,但不幸的是,我认为仍然存在跨域问题。 – Mottie 2010-04-21 14:18:54