javascript - 将 Javascript 传递到重定向的网页

标签 javascript redirect iframe web

我想在触发重定向时运行的代码是转到另一个网页(或本地 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/

相关文章:

Javascript 合并并重命名 Json 对象

javascript - SVG:为给定的每个 Angular 制作渐变以填充整个形状

javascript - 如何以编程方式检查特定元素的 CSS 样式?

.htaccess - 棘手的 .htaccess mod_rewrite 语法问题

django - 升级到 Django 3.2 LTS 后 iframe 停止工作

javascript - Chrome 扩展程序 : changing iframe source content after injected to a given page

javascript - 在javascript中将整数转换为数字的小数部分最有效的方法是什么?

apache - 我如何在 Apache http mod_rewrite 中使用原子作为路径变量?

php - 我如何重定向表数组以在 php 中显示其值

javascript - 停止在 designMode = "on"的文档内编辑某些元素;