javascript 从 iframe 内的脚本重定向整个页面的任何方式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5076666/
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-25 15:52:07  来源:igfitidea点击:

any way of redirecting the whole page from script inside iframe

javascriptasp.netiframeredirect

提问by Jimmy

As the topic says, Im looking for a way to redirect to another page, when i click a link in the iframe. i dont want just the iframe to redirect, but the whole window. is this possible?

正如主题所说,当我单击 iframe 中的链接时,我正在寻找一种重定向到另一个页面的方法。我不希望只重定向 iframe,而是整个窗口。这可能吗?

preferrably in javascript or in asp.net if possible

如果可能,最好使用 javascript 或 asp.net

EDIT: When i try the answers i get redirected to the source of the iframe, not to the source of the site the iframe lies on... Ill show you the code

编辑:当我尝试答案时,我被重定向到 iframe 的来源,而不是 iframe 所在网站的来源......我会向你展示代码

            function redirect() {
                window.top.location.href = "./Nyheter.html";
            }

As i dont want the code to be static, so that i can use it on many pages without changing the url, i want to do it this way, alt. get the url from db... but preferrably this way, Solution? Also, forgot to mention. The pages are not on the same domain, they are on different ones.. this could cause some problems.

因为我不希望代码是静态的,这样我就可以在许多页面上使用它而无需更改 url,所以我想这样做,alt。从 db 获取 url ......但最好是这样,解决方案?还有,忘记说了。这些页面不在同一个域中,它们在不同的域中。这可能会导致一些问题。

采纳答案by row1

You cannot use the window.top.location if your frame and the top level page are on different domains. A similar answer.

如果您的框架和顶级页面位于不同的域中,则不能使用 window.top.location。 一个类似的答案

回答by Andre Backlund

<a href="http://google.com" target="_parent">Click me!</a>

回答by Gatekeeper

javascript: window.top.location.href = "xxx"

javascript: window.top.location.href = "xxx"

回答by aiham

You should see this question which is pretty much identical: Redirect parent window from an iframe action

您应该看到这个问题几乎相同:从 iframe 操作重定向父窗口

I don't believe you can do it in ASP.net because this has to be done on the client side (ie. Javascript).

我不相信你可以在 ASP.net 中做到这一点,因为这必须在客户端(即 Javascript)完成。

window.top.location.href = "http://site.com";