Results 1 to 7 of 7

Thread: Calling a javascript function in a "a href=" statement

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Apr 2002
    Location
    Blue Mountains NSW Australia
    Posts
    160

    Calling a javascript function in a "a href=" statement

    Heya, Im a Javascript n00b, just curious as to why this script doesn't work , i know it's basic...

    This is the function in the head section of a HTML file...
    VB Code:
    1. <script language="JavaScript">
    2. <!--
    3.     Function callpic(picname)
    4.     { var h = 480;
    5.     var w = 640;
    6.     var l = (screen.width - w) / 2;
    7.     var t = (screen.height - h) / 2;
    8.     var fullpicname="http://users.bigpond.net.au/maniac/images/"+picname;
    9.     window.open('fullpicname', 'blank', 'width='+w+', height='+h+', top='+t+', left='+l);
    10.     }
    11. //-->
    12. </script>

    And this is a link which I want to call the function ...
    VB Code:
    1. <a href="http://***.***.net.au/***.htm" onClick="callpic('game-nfs1.jpg');">Image 1</a>

    any thoughts, opinions or ***'s appreciated
    Jack Daniels
    ICQ: 72074030
    VB 6.0

  2. #2
    Frenzied Member Rick Bull's Avatar
    Join Date
    Apr 2002
    Location
    England
    Posts
    1,444
    function should have a lowercase "F" at the start. Also if I understand what you are trying to do you may want to return false at the end of the onclick bit, so that the href won't be executed.

  3. #3
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    This should work

    Code:
    <a href="http://***.***.net.au/***.htm" onClick="javascript:callpic('game-nfs1.jpg');">Image 1</a>
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Apr 2002
    Location
    Blue Mountains NSW Australia
    Posts
    160
    Yeh thats what I'm trying to do , thanks, but whats the syntax for returning false? sorry im just doing this by trial and error...
    Jack Daniels
    ICQ: 72074030
    VB 6.0

  5. #5
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    for some reason VBF put a space between "java" and "script"...
    there shouldn't be a space there
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Apr 2002
    Location
    Blue Mountains NSW Australia
    Posts
    160
    Ok thanks ppl already got it working :w00t:

    suuuuure VBF...
    Jack Daniels
    ICQ: 72074030
    VB 6.0

  7. #7
    Frenzied Member Rick Bull's Avatar
    Join Date
    Apr 2002
    Location
    England
    Posts
    1,444
    To return false you do this:

    <a href="http://***.***.net.au/***.htm" onClick="callpic('game-nfs1.jpg'); return false;">Image 1</a>

    or:

    <script language="JavaScript">
    <!--
    function callpic(picname)
    { var h = 480;
    var w = 640;
    var l = (screen.width - w) / 2;
    var t = (screen.height - h) / 2;
    var fullpicname="http://users.bigpond.net.au/maniac/images/"+picname;
    window.open('fullpicname', 'blank', 'width='+w+', height='+h+', top='+t+', left='+l);
    return false;
    }
    //-->
    </script>
    <a href="http://***.***.net.au/***.htm" onClick="return callpic('game-nfs1.jpg');">Image 1</a>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width