问题描述
我尝试了一些不同的东西,但没有真正起作用,基本上我需要从iframe中获取当前位置/ url,获取我想要的部分并将其返回到URL中的散列。我怎样才能做到这一点在JavaScript?
选择正确的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属性也有意思:
I've tried a few different things but nothing really worked, basically i need to get the current location/url from the iframe, get the part i want and return it to the hash in the url. how can i do this in javascript?
Select the correct iframe element, pull out the src attribute, do your stuff, assign src to window.location.hash
var iframe = $('iframe');
var src = iframe.attr('src');
window.location.hash = src;
EDIT
If you want to get dynamic location from an iframe
you have to access contentWindow
property:
var iframe = $('iframe');
var contentWnd = iframe.attr('contentWindow');
var url = contentWnd.window.location.href;
window.location.hash = url;
also interesting reading on getting the contentWindow property:
这篇关于从iframe获取网址并在浏览器网址中更新哈希值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!