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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Attaching event handlers onload 1

Status
Not open for further replies.

Geee

Programmer
Apr 23, 2001
253
GB
Hi guys again, basically I have 2 scripts that both need to attach event handlers to different events onload. On attaches to "onkeyup" whilest the other attaches to "onchange". The scripts initialise as follows:

Code:
window.onload = attachEditHandlers;

function attachEditHandlers() {
	var objInput = document.getElementsByTagName('input');
	for (var iCounter=0; iCounter<objInput.length; iCounter++)
	objInput[iCounter].onchange = function(){return hasChanged(this);} //attach the onchange to each input field
}

and:

Code:
window.onload = attachFormHandlers;

function attachFormHandlers()
{
	var form = document.getElementById('edit') 

	if (document.getElementsByTagName)//make sure were on a newer browser
	{
		var objInput = document.getElementsByTagName('input');
		for (var iCounter=0; iCounter<objInput.length; iCounter++)
		objInput[iCounter].onkeyup = function(){return validateMe(this);} //attach the onchange to each input field
	}
	
}

The problem I have is that I cannot get both scripts to run simultaniously. Whichever script is included last is the only script that will work. both scripts work if I include them on their own or last. This is driving me insane so any help would be great.

Thanks in advance.

Gary

-Geeeeeeeeeeeeeeeeeeeeeeee-
 
Something like this works for me:

Code:
/* Onload code */
	if (typeof window.onload == 'function') {
		var _tempOnload = window.onload;
		window.onload = function() {
			_tempOnload();
			yourFunc1();
		}
	} else {
		window.onload = function () {
			yourFunc1();
		};
	}

So you'd have that code twice, with both function names included.

Hope this helps,
Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Hi Dan thanks for your swift reply. The problem I have is that the both scripts may or may not be included on the same page. Basically, one script performs validation on keyup, whilest the other script sets a variable if an input changes so, that I can generate an alert should the user navigate away without submitting the changes. Some pages have validation and not the alert script, some have the alert script and not the validation, and some have both. I was hoping that by using the event handlers I could just include the scripts without having to make any changes to any other areas of the code. It seems unfortunately that its not going to be as easy as that, unless you guys have any other ideas?

Thanks again.

G

-Geeeeeeeeeeeeeeeeeeeeeeee-
 
OK, my bad I didn't understand how that code worked at first glance. A terrible habit of mine you'd think I'd have the decency to read what someone has typed when they are giving their time up to help me out. Sorry!

Basically that script will look to see if something else has already been assigned to the onload event. If it has it will run both scripts on load, otherwise it will just load itself. Is that correct?

-Geeeeeeeeeeeeeeeeeeeeeeee-
 
Thanks so much Dan, you've really helped me this week :).

-Geeeeeeeeeeeeeeeeeeeeeeee-
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top