Link OnClick event

Discussion in 'Javascript' started by Miroslav Stampar [MCSD.NET / Security+], Jun 2, 2007.

  1. I have a problem that is best described by following code:

    <a href="http://www.google.com" onclick="new Image().src=\'http://
    www.example.com/process.cgi?p=1\'">Google</a>

    I want to call cgi script before a link is clicked. Upper example
    works as a standalone (AS IS), but...

    But, when I attach onclick event programmaticaly, clicked link loads
    to fast so image/CGI script doesn't have time to preload. Do you have
    any other solution for this problem?

    This is the sample page:

    <html>
    <head>
    <title>getElementById example</title>
    </head>

    <body>
    <div id="here">
    <td>
    <a href ="http://www.google.com">Google</a>
    <a href ="http://www.yahoo.com">Yahoo</a>
    </td>
    </div>

    <script>

    function fja()
    {
    var a = new Image();
    a.src='http://www.example.com/process.cgi?p=1';
    //alert("bla"); ---- when alert is put CGI manages to be called
    }

    var elementDiv = document.getElementById('here');
    var elementsA = elementDiv.getElementsByTagName('a');

    for(var i=0;i<elementsA.length;i++)
    {
    elementsA.onclick=fja;
    //alert(elementsA.href);
    }

    </script>

    </body>
    </html>
    Miroslav Stampar [MCSD.NET / Security+], Jun 2, 2007
    #1
    1. Advertising

  2. On Jun 2, 7:35 am, "Miroslav Stampar [MCSD.NET / Security+]"
    <> wrote:
    > I have a problem that is best described by following code:
    >
    > <a href="http://www.google.com" onclick="new Image().src=\'http://www.example.com/process.cgi?p=1\'">Google</a>
    >
    > I want to call cgi script before a link is clicked. Upper example
    > works as a standalone (AS IS), but...
    >
    > But, when I attach onclick event programmaticaly, clicked link loads
    > to fast so image/CGI script doesn't have time to preload. Do you have
    > any other solution for this problem?


    This seems like a really crazy thing to want to do :)

    > This is the sample page:
    >
    > <html>
    > <head>
    > <title>getElementById example</title>
    > </head>
    >
    > <body>
    > <div id="here">
    > <td>
    > <a href ="http://www.google.com">Google</a>
    > <a href ="http://www.yahoo.com">Yahoo</a>
    > </td>
    > </div>


    Try something like this...

    <script>

    function fja(url)
    {
    var a = new Image();
    a.src = 'http://www.example.com/process.cgi?p=1';
    // I've never used the onload attribute of an image but
    // it is supposed to have one.
    a.onload = function() {
    window.location.href = url;
    };
    }

    var elementDiv = document.getElementById('here');
    var elementsA = elementDiv.getElementsByTagName('a');

    for(var i=0; i<elementsA.length; i++)
    {
    elementsA.onclick = function() {
    fja(this.href);
    return false;
    };
    }

    </script>

    </body>
    </html>

    There are probably more elegant solutions. You could listen to the
    body element and see if it is clicked, determine if a link was
    clicked, prevent the browsers default behavior of following the link,
    load the image and when the image is loaded then follow the link. That
    would save doing all this attaching.

    Peter
    Peter Michaux, Jun 5, 2007
    #2
    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. Richard Thoms
    Replies:
    6
    Views:
    4,909
    Richard Thoms
    Dec 2, 2005
  2. Matt
    Replies:
    5
    Views:
    178
    Michael Winter
    Feb 12, 2004
  3. Jake Barnes
    Replies:
    1
    Views:
    157
  4. Miroslav Stampar [MCSD.NET / Security+]

    call CGI inside onclick event of a link

    Miroslav Stampar [MCSD.NET / Security+], Jun 2, 2007, in forum: Javascript
    Replies:
    1
    Views:
    356
    Miroslav Stampar [MCSD.NET / Security+]
    Jun 2, 2007
  5. Terry
    Replies:
    1
    Views:
    95
    Thomas 'PointedEars' Lahn
    Oct 15, 2007
Loading...

Share This Page