使用 Javascript 在 iframe 中导航 URL

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

Navigate URL in iframe with Javascript

javascripthtmliframe

提问by User

I have this code, to navigate a URL with javascript in an iframe, but it does not work. Why?

我有这个代码,可以在 iframe 中使用 javascript 导航 URL,但它不起作用。为什么?

For example, I want navigate to site.comwhen I click link1.

例如,我想site.com在单击 link1 时导航到。

<script language="javascript">
function nav()
{
window.navigate('http://site.com',target="DBox");
}   
</script>   


<a href="javascript:nav();">link1</a>

<iframe name="DBox" src="http://example" frameborder="0" style="width:100%;height:100%"></iframe> 

回答by noob

Simple HTML:

简单的 HTML:

<a target="DBox" href="http://site.com">link1</a>

Or with just call this with JavaScript:

或者用 JavaScript 调用它:

window.open("http://site.com", "DBox");

回答by crypto82

This will be navigate to url in the same window.

这将在同一窗口中导航到 url。

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