【发布时间】:2012-10-30 12:25:33
【问题描述】:
我想在触发重定向时运行的代码是转到另一个网页(或本地 html 文件,在这种情况下都可以),但是传递一些 javascript 以在该页面上运行,因为该页面可以正常工作在 iframe 中嵌入内容。需要这样做以允许我在重定向时指定 iframe 中的内容。
说得简单点。当您转到 website.com/about/ 时,我怎样才能做到这一点,它会重定向到 website.com/,并将 /about/ 的内容加载到 iframe 中?
<head>
<title> CodeBundle </title>
<script>
function home() {document.getElementById("loadedpage").src="home.html";}
function about() {document.getElementById("loadedpage").src="about.html";}
function reviews() {document.getElementById("loadedpage").src="reviews.html";}
function tutorials() {document.getElementById("loadedpage").src="tutorials.html";}
function blog() {document.getElementById("loadedpage").src="blog.html";}
</script>
</head>
<body>
<header>
<br><hr><font size=27><a onClick="home();">Code Bundle</a></font><br><hr>
<div ALIGN=RIGHT>
<font size=6> | <a onClick="about();">About</a> | <a onClick="reviews();">Reviews</a> | <a onClick="tutorials();">Tutorials</a> | <a onClick="blog();">Blog<a> |</font> <hr>
</div>
<iframe id="loadedpage" src=home.html width=100% height=100% frameborder=0>Iframe Failed to Load</iframe>
</header>
</body>
</body>
这是我的 index.html 用于 website.com/
我想写一个页面,当你转到website.com/about/时,它会重定向到website.com/,运行javascript函数about(),从而显示关于页面。
【问题讨论】:
-
我认为你可以在重定向页面上使用 window.opener。
-
请进一步解释一下
-
您可以将要访问的函数或变量放在第一页中(不要将其放在某个闭包中),然后使用 window.opener.FUNCTION/VARIABLE NAME 从其他页面访问,例如。 G。 window.opener.home().
-
我会测试一下,如果可行,请将其发布为答案,我会接受,谢谢
标签: javascript redirect iframe web