2

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

7 Answers 7

4

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>

2
  • Thanks, but i need open two pages. One in a tag, and one with javascript
    – marvinas
    Apr 27, 2011 at 13:02
  • 1
    the javascript: is not necessary.
    – mplungjan
    Apr 27, 2011 at 13:17
1

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.

2
  • that is correct to return false - which is why I return the result of the window.open and have the url in the href
    – mplungjan
    Apr 27, 2011 at 13:14
  • @mplungjan thanks, i know that this solution is working one, i just not sure if it works on all platforms/browser/and crazy pop blockers) That's why i commented that not sure about correctness of this solution.
    – Igor Milla
    Apr 27, 2011 at 13:33
1

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

5
  • You have a weird canonical syntax
    – CoolEsh
    Apr 27, 2011 at 13:05
  • @CoollEsh So because you do not understand it, you vote it down? Or do you just vote it down because I voted your example down? That is vendetta voting. Sad.
    – mplungjan
    Apr 27, 2011 at 13:09
  • @mplungjan its because I don't understand why your example is canonical?. It just allows to handle disabled JS but hard to read. If someone across internet disables JS then he will have enough disabilities and your handler not save him.
    – CoolEsh
    Apr 27, 2011 at 13:16
  • @CoolEsh it is called defensive coding and I think it is strange to vote down something that works better than if not coded that way and then post something that does not work at all. And it does not just handle disabled JS, it handles popup blockers which are now a days built into many browsers.
    – mplungjan
    Apr 27, 2011 at 13:20
  • @CoolEsh That is magnanimously of you :) Appreciated
    – mplungjan
    Apr 27, 2011 at 13:27
0

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

0
0

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>;
0
  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

0

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>

1
  • javascript: is not necessary unless there is a VBScript as the first script on the page. Also you should return false after the window.open
    – mplungjan
    Apr 27, 2011 at 13:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.