Click to See Complete Forum and Search --> : Catch onclick event


ivyshen
01-20-2003, 08:33 AM
What I need to do:
1. Need catch all onclick events in both IE and NS (currently dealing with IE browsers only)
2. Add some extra params to the URL that is going to be loaded

But I can not assign event handle via html, i.e
<a href="...." onClick="doThis();">link1</a>

So for IE browsers I have tried the following js code:
isNS4 = (document.layers) ? true : false;
isIE4 = (document.all && !document.getElementById) ? true : false;
isIE6 = (document.all && document.getElementById) ? true : false;
isNS6 = (!document.all && document.getElementById) ? true : false;
function AssignONCLICK() {
if ((isIE4) || (isIE6)) {
if (document.links) {
for (var d=0; d < document.links.length; d++) {
document.links[d].onclick=AddParam;
}
}
}
}
function AddParam() {
if ((isIE4) || (isIE6)) {
alert("AddParam called"); //this alert box appears fine!
var sHref = this.href + "~" + document.SingleCat.value + "~L=AD";
alert(this.href + "~" + document.SingleCat.value + "~L=AD");
window.location = sHref; //but the window still loads with the original URL (as difined in html anchor tag).
}
}

So the above code seems like it has catched the onClick event.
But how can I how can add extra params to the current URL and load it?
I presume that I sill have to cancle the onclick event first and then do the window.location=URL bit, but how can I cancle it?
Also from checking the props of this in function AddParam, it looks like a link obj rather than a event obj that I need, any ideas?

Many thanks in adv
ivy

khalidali63
01-20-2003, 09:32 AM
This is what your code seems to be doing.
append value of this element
document.SingleCat.value
at the end of the original element href value.
Do you intend to swap the href value alltogether with the value from this element
document.SingleCat.value

??

gil davis
01-20-2003, 09:36 AM
Originally posted by ivyshen
document.links[d].onclick=AddParam;Use:

document.links[d].onclick = function () {return AddParam();}
function AddParam() {
if ((isIE4) || (isIE6)) {
alert("AddParam called"); //this alert box appears fine!
var sHref = this.href + "~" + document.SingleCat.value + "~L=AD";
alert(this.href + "~" + document.SingleCat.value + "~L=AD");
window.location = sHref; //but the window still loads with the original URL (as difined in html anchor tag).
}
}

Use:

function AddParam() {
if ((isIE4) || (isIE6)) {
sHref = this.event.srcElement.href;
sHref += "~" + document.SingleCat.value + "~L=AD";
window.location = sHref;
return false;}
}