/    Sign up×
Community /Pin to ProfileBookmark

Catch onclick event

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

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@khalidali63Jan 20.2003 — 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

??
Copy linkTweet thisAlerts:
@gil_davisJan 20.2003 — [i]Originally posted by ivyshen [/i]

[B]document.links[d].onclick=AddParam;[/b][/quote]
Use:

[font=monospace]document.links[d].onclick = function () {return AddParam();}[/font]
[b]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).

}

}

[/b]
[/quote]

Use:

[font=monospace]function AddParam() {

if ((isIE4) || (isIE6)) {

sHref = this.event.srcElement.href;

sHref += "~" + document.SingleCat.value + "~L=AD";

window.location = sHref;

return false;}

}[/font]
×

Success!

Help @ivyshen spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.20,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...