Forum Moderators: open
My question is, if I do that will it add code to an already existing onclick event if one exists or will it over write it? I need it to add it.
This isn't as relevant on anchor tags as it is on form onsubmit events. I also need some code to add commands to onsubmit events for all forms pointing to an outside domain.
Thanks,
Brandon
function addHandler( iEl, evtS, iHandler ) {
if ( iEl.addEventListener ) { iEl.addEventListener( evtS, iHandler, false); }
else if ( iEl.attachEvent ) { iEl.attachEvent('on' + evtS, iHandler ); }
}
The add event first part goes as follows.
<script type="text/javascript">
if(window.addEventListener){ // Standard
var temp = document.getElementById("testLink").onclick;
document.getElementById("testLink").onclick = "";
if( temp != 'undefined' ){
document.getElementById("testLink").addEventListener('click', function(){
alert('test2');
}, false);
document.getElementById("testLink").addEventListener('click', temp, false);
}
}
else if(window.attachEvent) { // IE
var temp = document.getElementById("testLink").onclick;
document.getElementById("testLink").onclick = "";
if( temp != 'undefined' ){
// IE does this in reverse order for some reason
document.getElementById("testLink").attachEvent('onclick', temp, false);
document.getElementById("testLink").attachEvent('onclick', function(){
alert('test2');
}, false);
}
}
</script>
if (href.hostname != location.host)
The url syntax from .href on link elements and .action on form elements is the same, so same test should be Ok.
You need to consider that url may not contain the hostname ( relative urls ), maybe look for text other than location.host between \\ and \