Solved

window.open problem

Posted on 2004-09-24
14
2,635 Views
Last Modified: 2008-02-07
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
Comment
Question by:fkmfkm
  • 6
  • 2
  • 2
  • +3
14 Comments
 
LVL 36

Accepted Solution

by:
Zyloch earned 125 total points
ID: 12148981
Hi fkmfkm,

Try this:

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

Regards,
Zyloch
0
 
LVL 14

Expert Comment

by:boonleng
ID: 12149117
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
 
LVL 36

Expert Comment

by:Zyloch
ID: 12149120
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
Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

 
LVL 36

Expert Comment

by:Zyloch
ID: 12149125
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
 

Author Comment

by:fkmfkm
ID: 12149127
Zyloch

I get error
'return' statement outside of function.

Boon Leng

I want the link to work....
0
 
LVL 36

Expert Comment

by:Zyloch
ID: 12149134
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
 
LVL 36

Expert Comment

by:Zyloch
ID: 12149135
Of course, make sure your Javascript is enabled also.
0
 
LVL 14

Expert Comment

by:boonleng
ID: 12149251
Both mine and Zyloch code works :)
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 12149552
The error "return outside..." you get for this version:

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

0
 
LVL 25

Expert Comment

by:devic
ID: 12149981
in colection:
<a href="javascript:void(window.open('http://www.google.com'));">Link</a>
0
 
LVL 15

Expert Comment

by:Thogek
ID: 12152316
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
 

Author Comment

by:fkmfkm
ID: 12157354
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
 
LVL 36

Expert Comment

by:Zyloch
ID: 12157367
You can choose assisted answers. It's Split Points by this comments box
0
 
LVL 15

Expert Comment

by:Thogek
ID: 12157812
Some people just don't read instructions, eh?  ;-)
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question