Solved

window.open problem

Posted on 2004-09-24
14
2,632 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
 
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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…

744 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now