...

View Full Version : Javascript code that reads if a link has been clicked



jelly46
11-22-2012, 12:09 PM
I have a simple bit of code (contained in a CMS and cant be edited) that and needs to be read by Javascript in the header if it has been clicked.

Here is the code:


<div id="example" class="page-element-outer" style="text-align: left"><div class="page-element-inner" ><input type="image" name="Button1" id="Button" src="/getresource.axd?AssetID=16813" alt="Submit" style="text-align:left;" /></div></div>

Can you assist?

C

jmrker
11-22-2012, 12:57 PM
I have a simple bit of code (contained in a CMS and cant be edited) that and needs to be read by Javascript in the header if it has been clicked.

Here is the code:


<div id="example" class="page-element-outer" style="text-align: left"><div class="page-element-inner" ><input type="image" name="Button1" id="Button" src="/getresource.axd?AssetID=16813" alt="Submit" style="text-align:left;" /></div></div>

Can you assist?

C

Is that piece of code the part that cannot be edited?

jelly46
11-22-2012, 01:29 PM
Hello

Yes it is.

Philip M
11-22-2012, 04:09 PM
Is this what you mean?


<div id="example" class="page-element-outer" style="text-align: left">
<div class="page-element-inner" ><input type="image" name="Button1" id="Button" src="/getresource.axd?AssetID=16813" alt="Submit" style="text-align:left;" /></div>
</div>

<script type = "text/javascript">

var mydiv = document.getElementById('Button');
if (mydiv.addEventListener) { // all browsers except IE before version 9
mydiv.addEventListener("click", listener, false);
}

else {
if (mydiv.attachEvent) { // IE before version 9
mydiv.attachEvent ("onclick", listener);
}
}

function listener() {
alert (document.getElementById("Button").src)
}


</script>

Quizmaster: Which European capital is divided into 20 arrondissements?
Contestant: Sweden.

jelly46
11-23-2012, 10:00 AM
I think so, thanks Phillip

jmrker
11-23-2012, 03:01 PM
How is that going to work if it must be in the <head>
and you can not change anything in the <body>?
:confused:

Old Pedant
11-23-2012, 08:42 PM
Just have to do it at window.onload time.

Presumably, you don't want to interfere with any other events that are triggered by window.onload, so you have to do it in steps.

Maybe like this?


<head>
<script type="text/javascript">
function addEvent( addTo, eventName, listener )
{
if ( addTo.addEventListener != null )
{
addTo.addEventListener( eventName, listener, false );
} else {
addTo.attachEvent ("on" + eventName, listener);
}
}

function myButtonClickHandler( )
{
alert( this.id + " was clicked" ); // or whatever real code you want to use
}

function myOnLoadHandler( )
{
var mydiv = document.getElementById('Button');
addEventr( myDiv, "click", myButtonClickHandler );
}

addEvent( window, "load", myOnLoadHandler );
</script>

No?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum