将Javascript传递到重定向的网页

时间:2012-10-30 12:25:33

标签: javascript redirect iframe web

我想在触发重定向时运行的代码是转到另一个网页(或本地html文件,在这种情况下是可能的),但是通过一些javascript在该页面上运行,因为该页面正常工作在iframe中嵌入内容。需要这样做以允许我在重定向时指定iframe中的内容。

更简单。当我转到website.com/about/时,如何将其重定向到website.com/,并在iframe中加载/about/的内容?

<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(),以便显示关于页面。

2 个答案:

答案 0 :(得分:4)

您必须使用查询参数或片段标识符传递一些数据。

请参阅:

在任何一种情况下,您都会在网址中显示一些内容,它们将如下所示:

http://www.example.com/?page=about

或:

http://www.example.com/#about

或 - 这是最好的:

http://www.example.com/#!/about

因为它可以让您使网站可抓取。参见:


现在,在阅读您对the answer by theredled的评论之后,您“定期添加新内容并在嵌入式iframe中加载比每次编写新的html更快”我不得不问这个问题:< strong>你在网站上使用的是templating system吗?

请记住,制作支持AJAX的内容并使用片段标识符来显示正确的内容是不可行的,因为页面创建更容易(但不是),但因为用户体验更快,响应更快。例如,参见SoundJS库的网站:

当您点击顶部的PreloadJS链接时,请转到:

重新加载内容,地址栏会更改,但页面实际上是重新加载。 (您可以看到它是可以正常抓取的,因为如果您google for ReloadJS,它会显示在结果中。)

答案 1 :(得分:1)

通过用户会话传递内容?

然而,这是一个相当肮脏的案例,也许你已经知道:)

相关问题