PDA

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



jelly46
11-22-2012, 12:23 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

:confused:

Beverleyh
11-22-2012, 01:09 PM
2 things;

what needs to be read when what is clicked? (and what do you mean exactly when you say "read by javascript"?)

Also, how can you add javascript if you cant edit the CMS header?

Beverleyh
11-22-2012, 01:17 PM
Thinking on, do you mean an onclick event in javascript, like this?: http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_onclick

If so, what exactly do you want to achieve "onclick"?

The previous question still stands too though - how can you add javascript if you cant edit the CMS? Or do you mean something else?

jelly46
11-22-2012, 01:24 PM
Thinking on, do you mean an onclick event in javascript, like this?: http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_onclick

If so, what exactly do you want to achieve "onclick"?

The previous question still stands too though - how can you add javascript if you cant edit the CMS? Or do you mean something else?

Hello

The code you see in this topic, is a plugin in the CMS i work on, I cannot edit this code. But i do have access to the header and i need the JS to read the clicks on the link or div id.

Here is some JS i've been working on:
<script type="text/javascript">
document.getElementById("Button").addEventListener("click", callback, true);

function callback() {
alert("clicked");

return false;
}
</script>

Beverleyh
11-22-2012, 03:04 PM
Is this resolved then? was it just the javascript onclick event that you needed help identifying?

jelly46
11-22-2012, 03:12 PM
Is this resolved then? was it just the javascript onclick event that you needed help identifying?

Hello, no not yet

jscheuer1
11-22-2012, 03:27 PM
That won't work in the head because the button doesn't exist yet. If the page already uses jQuery, this should work:


<script type="text/javascript">
jQuery(function($){
$('#Button').click(function(e){
alert('clicked');
return false;
});
});
</script>

I say should, because if the CMS has already assigned it an on click function, that may still fire and may fire first. And that may vary by browser.

A rough equivalent if jQuery isn't on the page would be:


<script type="text/javascript">
if (window.addEventListener){
window.addEventListener('load', function(){
document.getElementById('Button').addEventListener('click', function(e){alert('clicked');e.preventDefault();}, true);
}, false);
}
else if (window.attachEvent){
window.attachEvent('onload', function(){
document.getElementById('Button').attachEvent('onclick', function(){alert('clicked');event.returnValue = false;});
});
}
</script>

But again, if the CMS has already assigned an on click event to it, that may take precedence. And that may vary by browser.

The browser cache may need to be cleared and/or the page refreshed to see changes.

If you want more help, please include a link to the page on your site that contains the problematic code so we can check it out.

jelly46
11-22-2012, 03:52 PM
That won't work in the head because the button doesn't exist yet. If the page already uses jQuery, this should work:


<script type="text/javascript">
jQuery(function($){
$('#Button').click(function(e){
alert('clicked');
return false;
});
});
</script>

I say should, because if the CMS has already assigned it an on click function, that may still fire and may fire first. And that may vary by browser.

A rough equivalent if jQuery isn't on the page would be:


<script type="text/javascript">
if (window.addEventListener){
window.addEventListener('load', function(){
document.getElementById('Button').addEventListener('click', function(e){alert('clicked');e.preventDefault();}, true);
}, false);
}
else if (window.attachEvent){
window.attachEvent('onload', function(){
document.getElementById('Button').attachEvent('onclick', function(){alert('clicked');event.returnValue = false;});
});
}
</script>

But again, if the CMS has already assigned an on click event to it, that may take precedence. And that may vary by browser.

The browser cache may need to be cleared and/or the page refreshed to see changes.

If you want more help, please include a link to the page on your site that contains the problematic code so we can check it out.


Thanks a lot for this John I will give it a go and feed back.