从iframe获取网址并在浏览器网址中更新哈希值 [英] Get url from iframe and update hash in browser url

查看:116
本文介绍了从iframe获取网址并在浏览器网址中更新哈希值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了一些不同的东西,但没有真正起作用,基本上我需要从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属性也有意思:

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


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:

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

这篇关于从iframe获取网址并在浏览器网址中更新哈希值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆