[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2641
  • Last Modified:

window.open problem

I have a simple javascript

<a href="javascript:window.open('http://www.google.com');">Link</a>

But each time i click this link my main browser change to

[object]

why is this happenning ? how to prevent it ?

Thank you
0
fkmfkm
Asked:
fkmfkm
  • 6
  • 2
  • 2
  • +3
1 Solution
 
ZylochCommented:
Hi fkmfkm,

Try this:

<a href="#" onclick="window.open('http://www.google.com');return false;">Link</a>

Regards,
Zyloch
0
 
boonlengCommented:
Or this to avoid the link from execute.

<a href="javascript:void(0)" onclick="window.open('http://www.google.com');return false;">Link</a>

Regards.
Boon Leng
0
 
ZylochCommented:
Just as clarification, boonleng is using javascript:void(0) which under normal circumstances, will do nothing, if the user has Javascript on or off. This is recommended if you want that. I used #, which will bring the user back to the top of the page if they have Javascript disabled. If the user has Javascript enabled however, both will be the same because of the return false;

Whew, a bunch of philosophical ideas there =)

Regards...
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
ZylochCommented:
As a side note, because of the way this behaves, you can test the user for Javascript:

<a href="nojavascript.html" onclick="window.location.href='somewhere.html';return false;">Visit Our Site that needs Javascript.</a>

If the user has Javascript, onclick will fire, but if not, they will be taken to nojavascript.html

Regards...
0
 
fkmfkmAuthor Commented:
Zyloch

I get error
'return' statement outside of function.

Boon Leng

I want the link to work....
0
 
ZylochCommented:
My first one was tested on IE6 and Mozilla Firefox 0.9.3

If you don't believe, open up Notepad, just plop this:

<a href="#" onclick="window.open('http://www.google.com');return false;">Link</a>

in there, save as an html document, and open it.
0
 
ZylochCommented:
Of course, make sure your Javascript is enabled also.
0
 
boonlengCommented:
Both mine and Zyloch code works :)
0
 
ZvonkoSystems architectCommented:
The error "return outside..." you get for this version:

<a href="javascript:window.open('http://www.google.com');return false">Link</a>

0
 
devicCommented:
in colection:
<a href="javascript:void(window.open('http://www.google.com'));">Link</a>
0
 
ThogekCommented:
First of all, is there a reason you're using JavaScript for this link?  Your posted code above isn't doing anything to control the size of the new window, so it seems you could get much the same effect with something simpler like:

    <a href="http://www.google.com/" target="_blank">Link</a>

If you are controlling aspects of the new windows that you just didn't include above,...
Just as another marble to roll around in the ring, I personally tend to like:

    <a href="http://www.google.com/" onclick="window.open(this.href, '_blank'); return false;">Link</a>

That way, the link works and goes to the correct page regardless of whether JavaScript is currently supported, but will do so by way of a new window if JavaScript is supported.
0
 
fkmfkmAuthor Commented:
Opps...

My mistake..Zvonko pointed it to me...some typo error...

Too bad i just can accept one solution.Zyloch one cause he gave the first solution. You guys have been great.

Thogek you also have a nice solution.

thanks..
0
 
ZylochCommented:
You can choose assisted answers. It's Split Points by this comments box
0
 
ThogekCommented:
Some people just don't read instructions, eh?  ;-)
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 6
  • 2
  • 2
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now