开发者

open new window with javascript in current page

how open new window with javascript in current page (using a tag!)?

i have script, but this no work, why?

<a href="home.html" onclick="window.open('http://g开发者_如何学JAVAoogle.com/', '_self')">Go</a>;


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>


you can add return false to stop browser from starting default behavior of an a tag with no href.

<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.


Just change it to

<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)

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.

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


Put a # in the href string. That should work.


You need to put a # in the href as the blank href is confusing the browser on the link.

<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

<a href="#" onclick="window.open('http://google.com/', '_blank')">Go</a>;


  1. If you wish to "load" a URL in the current page just execute location.href = "www.google.com"

  2. If you wish to "create" a new window execute < a href="#nogo" onclick="window.open('http://google.com/');return false;">Go< /a>

Thanks


open new window with javascript in current page

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>

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>

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜