我想在触发重定向时运行的代码是转到另一个网页(或本地 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>
这是我的 website.com/的 index.html
我想写一个页面,这样当你转到website.com/about/
时它重定向到 website.com/
运行 javascript 函数 about()
, 以显示关于页面。
最佳答案
您必须使用查询参数或片段标识符传递一些数据。
参见:
无论哪种情况,您都会在 url 中显示一些内容,它看起来像:
http://www.example.com/?page=about
或:
http://www.example.com/#about
或者 - 这将是最好的:
http://www.example.com/#!/about
因为它可以让您使网站可抓取。见:
现在阅读您对 the answer by theredled 的评论后您“定期添加新内容并将其加载到嵌入式 iframe 中比每次都编写新的 html 更快” 我不得不问:您不是在使用 templating system 吗?在您的网站上?
请记住,制作 AJAX 加载内容和使用片段标识符显示正确的内容并不是因为页面创建更容易(实际上不是),而是因为用户体验更快、响应更快。例如,请参阅 SoundJS 库的网站:
当您点击顶部的 PreloadJS 链接时,您将转到:
内容重新加载,地址栏改变,但页面实际上没有重新加载。 (您可以看到它是正确可抓取的,因为如果您 google for ReloadJS ,它就会显示在结果中。)
关于javascript - 将 Javascript 传递到重定向的网页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13138937/