I see this kind of questions on these forums pretty often :
~how 2 add something to ... event handler
or
~how 2 remove some handler
áááááááááááááááááááá
i'll talk about onclick handler, but it could be any of them..
& another thing: this code works in ie only (at least onclick is not supported by nc4)
0. have a look at my faq about enabling/disabling "readonly" attribute of form fields: faq216-795 but there i just removed handler with
[color green]handler=new Function()[/color]
1. To remove defined handler just write
[color green]onclick=null[/color] or like i did it in the pointed faq
2. a bit harder: How to add to/remove something from event handler:
just copy-paste this code & play with it
[color green]
<html>
<head>
<title>changing function code</title>
<script>
[color gray]
/*********************
the main monster: this function gets handler's body
*********************/[/color]
function getFuncBody(funcPtr) {
[color gray]//handler's body[/color]
var str=funcPtr.toString();
[color gray]// removes everything including first "{" (e.g. function name(){)[/color]
str=str.replace(/[^{]+{/,"");
[color gray]// removes last }[/color]
str=str.substring(0,str.length-1);
[color gray]// guess what )[/color]
return str;
}
[color gray]//onclick handler:[/color]
function clicker(){
one();one()
one()
}
[color gray]//function that is used in onclick handler:[/color]
function one() {
var ii=0
if (!ii){alert('one')}
}
[color gray]
/*********************
function wich adds to current handler some actions: this time it is just alert('two') but u can call some other function.. play around
*********************/[/color]
function doIt() {
document.onclick=new Function(
getFuncBody(document.onclick)+";alert('two')" );
}
[color gray]
/*********************
function wich removes all 'one()' function calls, but u can do whatever u whant
*********************/[/color]
function doItagain() {
var str=getFuncBody(document.onclick)
str=str.replace(/one\(\)/g,';') //removal
document.onclick=new Function(str);
}
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.