简体   繁体   中英

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://google.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>

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 (ie 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

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.

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>

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM