473,394 Members | 1,829 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Link OnClick event

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[i].onclick=fja;
//alert(elementsA[i].href);
}

</script>

</body>
</html>

Jun 2 '07 #1
1 3647
On Jun 2, 7:35 am, "Miroslav Stampar [MCSD.NET / Security+]"
<miroslav.stam...@gmail.comwrote:
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[i].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

Jun 5 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Matt Adams | last post by:
As well known I could specify the text color in the body tag like: <BODY TEXT=WHITE LINK=WHITE VLINK=RED ALINK=WHITE> What I want to achieve now is that always (!) the text of the last visited...
3
by: Carl Gilbert | last post by:
Hi How can I use a link to both run a peice of script and link to another page? So far I have the following: ----------------------------------------------------------------------- <A...
7
by: bill | last post by:
Is there any way to make a link (as marked by an <A>-tag, that is), behave exactly like a submit button? Many thanks! -bill
6
by: Tom Braun | last post by:
Hello! My first post here... I need to monitor if someone clicked on some (any) link in a subframe. Due to certain restrictions, the only place I can put some JavaScript is in the main...
21
by: news.btinternnet.com | last post by:
I can do this in IE myLink.click(); //Invoking the handler as if the user had clicked on the link themselves. I need to be able to do the same in Netscape Navigator, can anyone help ?
3
by: Spartanicus | last post by:
I have a list of links using the following construct: <a href='#artane' onclick='pos(1107,281)'>Artane</a> The links offer additional functionality through JS. There are no actual fragment IDs...
10
by: Carlos Araya | last post by:
I have the following link on a web page <p class="menuitem"><a href="#" onclick="loadFragment('http://rivendellweb.net/fortress/home', 'index')" title="The Fortress Home">The Fortress...
11
by: nma | last post by:
Hi How can I make the anchor link once I do onclick? <td width="<?php echo ($cellwidth); ?>" height="17" bgcolor="#00ff00" onMouseOver="this.style.background ='red'" ...
5
by: Xu, Qian | last post by:
Hello All, I have some problem by simulating a link click using javascript. The webpage uses a js-library named interface (jQuery like) ------------------------------ <a id="foo" href="#">Try...
9
by: skultetc | last post by:
Hey all, I have a div displayed as a block with an onclick event that shows/ hides a different div underneath it. There is a link within the first div that takes the user to a different page. My...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.