简而言之,我们有一个带有iframe的页面,我想动态定位页面URL。
为简单起见,页面位于http://1/2/3.aspx iframe将位于3.aspx页面上,而iframe指向完全不同的URL,例如http:// 100/101 / 102.html
我想将iframe定位到http:// 1/2 / “some URL”
一位同事建议使用window.location.parent进行内联javascript,但我无法弄清楚如何实现这一点。 在链接的href标记中使用通常的../只会导致我在框架的上下文中向上导航(到101,100,依此类推)。
一如既往,感谢社区为您的时间。
编辑:如果可能的话,我想使用内联javascript。
In short, we have a page with an iframe, and I want to target the page URL dynamically.
For simplicity, the page is located at http://1/2/3.aspx The iframe would be on the 3.aspx page, and the iframe points to a completely different URL, let's say http://100/101/102.html
I want to target the iframe to go to http://1/2/"some URL"
A coworker suggested inline javascript using window.location.parent, but I can't figure out how to implement this. Using the usual ../ in the link's href tag only results in me navigate up within the frame's context (to 101, 100, so on).
As always, thank you to the community for your time.
EDIT: I would like to use inline javascript for this if at all possible.
最满意答案
我想你的意思是这样的
<iframe id="ifr" src=""></iframe> <script> document.getElemenById("ifr").src = "http://1.2.3/page.html"; </script>更新
要使用iframe父级的位置数据,您可以这样做
<iframe id="ifr" src=""></iframe> <script> var parenturl = parent.location.href; var newurl = parenturl.replace("from-this","to-this"); document.getElemenById("ifr").src = newurl; </script>I guess you mean something like this
<iframe id="ifr" src=""></iframe> <script> document.getElemenById("ifr").src = "http://1.2.3/page.html"; </script>Update
To use location data from the iframe's parent, you can do like this
<iframe id="ifr" src=""></iframe> <script> var parenturl = parent.location.href; var newurl = parenturl.replace("from-this","to-this"); document.getElemenById("ifr").src = newurl; </script>更多推荐
发布评论