javascript 在当前页面用javascript打开新窗口
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5804465/
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
open new window with javascript in current page
提问by marvinas
how open new window with javascript in current page (using a tag!)?
如何在当前页面中使用 javascript 打开新窗口(使用标签!)?
i have script, but this no work, why?
我有脚本,但这不起作用,为什么?
<a href="home.html" onclick="window.open('http://google.com/', '_self')">Go</a>;
回答by Thomas Shields
Why use JavaScript? why not: <a href="http://google.com" target="_blank">Go</a>
To open a second new window as well,
<a href="http://google.com" target="_blank" onclick="window.open('http://google.com')">Go</a>
为什么要使用 JavaScript?为什么不:<a href="http://google.com" target="_blank">Go</a>
还要打开第二个新窗口,
<a href="http://google.com" target="_blank" onclick="window.open('http://google.com')">Go</a>
回答by Igor Milla
you can add return false
to stop browser from starting default behavior of an atag with no href.
您可以添加return false
以阻止浏览器启动没有 href的a标签的默认行为。
<a href="" onclick="window.open('http://google.com/', '_self'); return false;">Go</a>;
but i will not suggest this is a right way to go.
但我不会建议这是一条正确的道路。
回答by mplungjan
Just change it to
只需将其更改为
<a href="http://google.com/" target="_self">Go</a>
<a href="http://google.com/" target="_self">Go</a>
you will not be able to turn current page into a popup (i.e. remove chrome)
您将无法将当前页面变成弹出窗口(即移除 chrome)
If you want to open a new window, the canonical (according to me) syntax would be
如果你想打开一个新窗口,规范的(根据我)语法是
<a href="http://google.com/" target="_blank"
onclick="var w=window.open(this.href,this.target); return w?false:true">Go</a>
to handle popup blockers - the href is followed if the window.open fails so the page will be loaded regardless.
处理弹出窗口阻止程序 - 如果 window.open 失败,则遵循 href ,因此无论如何都会加载页面。
To open a page in the current window AND popup a new window you want
在当前窗口中打开一个页面并弹出一个你想要的新窗口
<a href="http://google.com/"
onclick="window.open('http://msn.com','_blank')">Go</a>
Here you do NOT want to return false since you want the link to be followed
在这里,您不想返回 false,因为您希望跟踪链接
回答by Harry Joy
open new window with javascript in current page
在当前页面用javascript打开新窗口
How can one open a new window in current window.
如何在当前窗口中打开一个新窗口。
To open site in same page use:
要在同一页面中打开站点,请使用:
<a href="http://google.com">Go</a>
or
或者
<a href="#" onclick="window.open('http://google.com/', '_self')">Go</a>;
or Use
或使用
<a href="http://google.com" target="_blank">Go</a>
<a href="http://google.com" target="_blank">Go</a>
for opening site in new window.
用于在新窗口中打开网站。
or following will also work to open site in new window:
或以下也可以在新窗口中打开站点:
<a href="" onclick="window.open('http://google.com/')">Go</a>
<a href="" onclick="window.open('http://google.com/')">Go</a>
回答by mauritslamers
Put a # in the href string. That should work.
在 href 字符串中放置一个 #。那应该有效。
回答by Alan Barber
You need to put a # in the href as the blank href is confusing the browser on the link.
您需要在 href 中添加 #,因为空白的 href 会混淆链接上的浏览器。
<a href="#" onclick="window.open('http://google.com/', '_self')">Go</a>;
but if you want to open a new window you need to use _blank
但是如果你想打开一个新窗口,你需要使用 _blank
<a href="#" onclick="window.open('http://google.com/', '_blank')">Go</a>;
回答by Shirsendu
If you wish to "load" a URL in the current page just execute location.href = "www.google.com"
If you wish to "create" a new window execute < a href="#nogo" onclick="window.open('http://google.com/');return false;">Go< /a>
如果您希望在当前页面中“加载”一个 URL,只需执行 location.href = "www.google.com"
如果您想“创建”一个新窗口,请执行 <a href="#nogo" onclick="window.open('http://google.com/');return false;">Go</a>
Thanks
谢谢