Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

I wonder if someone knows how to do 3

Status
Not open for further replies.

Izzo

Technical User
Feb 22, 2001
38
US
I wonder if someone knows how to do this.

I'm trying to figure out how to remove the complete event and handler -
onLoad="Bob()" as in Example 1 below - if they are the only ones in the body tag.

Or, remove "only" the handler - "Bob()" if another handler is present as in Example2 below.

Example1 <body onLoad=&quot;Bob()&quot;>
Example2 <body onLoad=&quot;Bob();Bill()&quot;>

These examples could as well be like:

People=&quot;Bob&quot;
People=&quot;Bob&quot;,&quot;Bill&quot;

I know how to access the elements in the DOM but I don't know how to make the script see if they both exist or not and if they do delete only the one specified to be removed and leave the other one.

Bob


Mark
 
Typo -
Please ignore this section of the post:



These examples could as well be like:

People=&quot;Bob&quot;
People=&quot;Bob&quot;,&quot;Bill&quot;
 
1. onload=null
it will remove any handler that was set

2. adding smth to pre-defined handler:

<html>
<head>
<title>changing function code</title>
<script>
function getFuncBody(funcPtr) {
var str=funcPtr.toString(); //function body
str=str.replace(/[^{]+{/,&quot;&quot;);// removes everything including first &quot;{&quot; (e.g. function one(){)
str=str.substring(0,str.length-1); // removes last &quot;}&quot;
return str;
}

function one() {
var ii=0
if (!ii){alert('one')}
}

function doIt() {
document.onclick=new Function(
getFuncBody(document.onclick)+
&quot;;alert('two')&quot; );
}

document.onclick= one;
</script>
</head>
<body bgcolor=&quot;#FFFFFF&quot;>
<input type=button onclick=&quot;doIt()&quot; value=button>
</body>
</html>

now i'm workin on removing smth from handlers

will let u know.. Victor
 
yeah, got it to work, but pretty ugly:

<html>
<head>
<title>changing function code</title>
<script>
function getFuncBody(funcPtr) {
var str=funcPtr.toString(); //function body
str=str.replace(/[^{]+{/,&quot;&quot;);// removes everything including first &quot;{&quot; (e.g. function one(){)
str=str.substring(0,str.length-1); // removes last &quot;}&quot;
return str;
}

function clicker(){
one()
}

function one() {
var ii=0
if (!ii){alert('one')}
}

function doIt() {
document.onclick=new Function(
getFuncBody(document.onclick)+
&quot;;alert('two')&quot; ); alert(document.onclick)
}


function doItagain() {
var start,nex,strr,rest
var str=getFuncBody(document.onclick);// alert(str)
strr=&quot;one()&quot;
start=str.indexOf(strr)//alert(str.indexOf('one()'))
nex=start+strr.length
rest=str.length-strr.length
str=str.substr(0,start)+';'+str.substr(nex,rest)
/**
var st=new RegExp(&quot;one()&quot;);//alert(st)
if (str.match(st)) alert('matched')
str.replace(st,&quot;&quot;); alert(str)
**/

document.onclick=new Function(str);
}

document.onclick= clicker;
</script>
</head>
<body bgcolor=&quot;#FFFFFF&quot;>
<input type=button onclick=&quot;doIt()&quot; value=add><br>
<input type=button onclick=&quot;doItagain()&quot; value=remove><br>
</body>
</html>


u c, i'm not good in regexps, (but i'm workin on it :))
red lines in the script - this is how i wanted to replace every &quot;one()&quot; entries from function body, but it doesnt work for me :-(
culd anybody plz tell me where's my error?

i did it thru substrings (woo.. ugly-ugly)

help me get this code 2 pretty view!!

thanks Victor
 
hi vituz :)))
happy to be back, and happy to help you (and izzo !) with regexps :)
so you want to retrieve all &quot;one&quot; in a string and remove them, right ?
i finally found out a way to do this :
re = /(one)/;
// this is the reg exp itself
before_one = RegExp.leftContext
// this will return what was BEFORE the first &quot;one&quot; encountered. You have to use RegExp and not re, but i tested, it finds the string (i don't know HOW tho !)
after_one = RegExp.rightContext;
// end of the string. Becareful that if you have another &quot;one&quot; in the remaining string, it won't be removed - so this code only removes the FIRST &quot;one&quot; you encounter - but it shouldn't be that hard to change it to remove all the &quot;one&quot;s ;]]
 
hie again :)))))))
yeah, iza, u almost got it :)
actually, i wanna remove every one() entries..

i'm takin a look at ur code, thanks a lot :)

but it wuld be great if somebody wuld show me how to remove all of them

thanks again Victor
 
well to remove all of them i guess it's only a while on the right part :
re = /(one)/;
beginning = &quot;&quot;
while (re.test(input_string))
{
beginning+=RegExp.leftContext
input_string= RegExp.rightContext
}
answer = beginning+input_string

tested - works fine !
ok have to work now, i'm laaaaaaaaaaate, so i hope you've got everything !! i'll pop by later on - before i leave, tonight
 
iza,
thank u very much!
i appreciate ur help, star for ya, but (u'll excuse me, right?) i'll use my own solution :)

how's that in BloodHoundGang's theme?

..
~hello my name is vituz
~and i'm a dumb white guy
..
..lalala..

i replaced it almost from the first time :)-)) but forgot to apply my removal:

function doItagain() {
var str=getFuncBody(document.onclick)
str=str.replace(/one\(\)/g,'')
document.onclick=new Function(str)
}


replace doItagain function in my prev. example with this last final version :)

now it works nice!! Victor
 
i don't mind you're not using &quot;my&quot; solution - i personnaly think it's always better to use a solution you feel really at ease with, even if it doesn't seem to be the 'best' one (and i'm not saying the reg exp one is better eiter, personnaly i would have used the substr version - with a while !!!)
 
take a look, i've just submitted a faq on this subject:
faq216-1063 Victor
 
Wow you guys are amazing. I'm trying to understand this code but I'm just not there yet.
I made a program (an extension) for Dreamweaver that inserts some head code and a body onLoad event into a .htm document.This program also allows for the removal of the script.
I can remove the head script no problem, and I can remove the onLoad and its handler no problem.
The problem is if at a later time another handler is added to this onLoad event my removal script removes the event and &quot;both&quot; handlers.
I don't know how to make the script see if another handler exists, and if it does remove only remove the one originally inserted by my program. which by the way will always be named the same thing, but the others could be anything.

Izzo





 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top