href with onClick javascript problem in new window

Discussion in 'Javascript' started by Mike, Apr 30, 2004.

  1. Mike

    Mike Guest

    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/page1.pl";
    }
    </script>
    ...
    <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() {
    window.open("/cgi-bin/page1.pl", "_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/page1.pl";
    }
    </script>
    ...
    <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.

    Thanks,
    Mike
    Mike, Apr 30, 2004
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Joshua Beall
    Replies:
    26
    Views:
    220,358
    AndreiKaa
    Jun 29, 2007
  2. CRON
    Replies:
    24
    Views:
    200,690
    Adrienne Boswell
    Jun 20, 2006
  3. Mike
    Replies:
    5
    Views:
    228
    Thomas 'PointedEars' Lahn
    May 18, 2004
  4. Replies:
    2
    Views:
    459
  5. Vincent van Beveren

    BASE HREF and A HREF="#" onclick="..."

    Vincent van Beveren, Jul 6, 2006, in forum: Javascript
    Replies:
    2
    Views:
    312
    Vincent van Beveren
    Jul 6, 2006
Loading...

Share This Page