Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > href with onClick javascript problem in new window

Thread Tools

href with onClick javascript problem in new window

Posts: n/a
I am new to javascript and the DOM. I've searched google groups,
and although I saw numerous discussions about javascript and
hrefs, I did not see a solution to this problem. Thank you in
advance for any help you might offer.

GOAL: (very simple) Provide a hyperlink which, when clicked,
calls a javascript function which opens a new URL.

I want a hyperlink instead of a button, so that the user can
decide whether to open in the same window or in a new window (or
new tab) via their browser controls.

PROBLEM: The following code works fine if I click to open in
the same window, but if I click the browser option to open in a
new window, the new window tries to open the href URL (the
onClick function does get executed, but seems to be ignored).
I've run this in IE6, NS7, and Opera.

<script language="javascript">
function NewPage() {
location.href = "/cgi-bin/";
<a href="noJS.htm" onClick="NewPage(); return false;">

I have also tried href="#", href="javascript:void(0);", href="",
but in all cases, the new window always attempts to open the href
text (in NS 7, if the href contains the "javascript:" pseudo-URL,
it does not even give the option of opening in new window). I do
realize that "javascript:" in an href is not good practice.

I also tried the following, with the same results:
function NewPage() {"/cgi-bin/", "_top");

I also tried setting the href from in the javascript function,
with the same results; it works in the same window, but when
opening in new window, it tries to open the original href text,
not what it was changed to in the NewPage() function.
<script language="javascript">
function NewPage(index) {
document.anchors[index].href = "/cgi-bin/";
<a name="x" href="noJS.htm" onClick="NewPage(index); return true;">
// index is the appropriate index for this anchor

Again, any help or pointers would be much appreciated.

Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
JavaScript refresh: <a href="#" onClick="window.location.reload( true );">Reload</a> Joshua Beall HTML 26 06-29-2007 06:31 PM
BASE HREF and A HREF="#" onclick="..." Vincent van Beveren Javascript 2 07-06-2006 08:33 AM
href="javascript:func()" vs href="#" onclick="javascript:func()" CRON HTML 24 06-20-2006 08:05 PM
onClick method question (this.href and document.location.href) Javascript 2 02-03-2005 02:38 PM
href with onClick javascript problem in new window Mike Javascript 5 05-18-2004 05:13 PM