javascript 元刷新重定向到顶部框架

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11690253/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 14:01:11  来源:igfitidea点击:

meta refresh redirect to top frame

javascripthtmlredirectrefreshmeta-tags

提问by Malachi

I have the following code:

我有以下代码:

<html>
<head>
<title>title of this stuff</title>
<script language="JavaScript"> 
  if (top != self) top.document.title = document.title;
</script>
<meta http-equiv="refresh" content="2;     URL=javascript:window.open('certainpage.html','_top');">
</head>
<body>
Body of this page
</body>
</html>

and this doesn't work. I've googled for this and come to the same conclusion everywhere: this should work. But it doesn't. Can anyone help me out why this page isn't: 1. refreshing as long as I have the javascript in there (and yes, js is enabled in my browser) 2. refreshing to the new page in the top frame

这不起作用。我已经用谷歌搜索了这个并在任何地方得出相同的结论:这应该有效。但事实并非如此。谁能帮我看看为什么这个页面不是: 1. 只要我有 javascript 就刷新(是的,我的浏览器中启用了 js) 2. 刷新到顶部框架中的新页面

Any help would be appreciated!

任何帮助,将不胜感激!

回答by

Javascript won't work in the refreshmeta tag like that.

Javascript 不会在这样的刷新元标记中工作。

As you're using javascript anyway, keep it simple like this:

无论如何,当您使用 javascript 时,请保持简单,如下所示:

<script type="text/javascript">
    window.top.location = 'http://domain.tld/whatever/';
</script>

But there's also a better (because smarter) way to do it. This doesn't require you to hard-code the URL for each page. It checks if the page is topmost and if not, if calls the page's URL to the top:

但也有更好的(因为更聪明)的方法来做到这一点。这不需要您对每个页面的 URL 进行硬编码。它检查页面是否位于最顶层,如果不是,则调用页面的 URL 到顶部

<script type="text/javascript">
    if(window.top.location != window.location) 
    {
        window.top.location.href = window.location.href; 
    }
</script>

And if you would prefer to completely avoid using javascript (which some users will have disabled), there's also an even simpler way to do it. Add the following to your headsection and all links on that page will open "topmost":

如果您希望完全避免使用 javascript(有些用户会禁用它),还有一种更简单的方法可以做到。将以下内容添加到您的头部部分,该页面上的所有链接将打开“最顶层”:

<base target="_top">

All you have to do is to choose one of these three options. All of them should get you going just fine.

您所要做的就是从这三个选项中选择一个。所有这些都应该让你一切顺利。