Link OnClick event

  • Thread starter Miroslav Stampar [MCSD.NET / Security+]
  • Start date
M

Miroslav Stampar [MCSD.NET / Security+]

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>
 
P

Peter Michaux

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,996
Messages
2,570,238
Members
46,826
Latest member
robinsontor

Latest Threads

Top