SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Wizard
    Join Date
    Apr 2002
    Posts
    2,307
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)

    general IEifying, and addEventListener and ontheevent=...

    what needs doing to the following to make it more generally portable (especially for IE)?:
    Code:
    function setHowFarFormUp() {
    	var instru = document.getElementById("instr");
    	instru.parentNode.removeChild(instru);
    	document.forms.howfar.onsubmit = howFarSubmission;
    	var textfield=document.getElementsByName("postcode")[0];
    	if(textfield.hasfocus != true) outOfField();
    	else if(textfield.value == tFInstr) textfield.value = "";
    	removeErrorMessage();
    	textfield.addEventListener("focus",
    		function() {
    			textfield.setAttribute("style", "color:#000");
    			if(textfield.value == tFInstr)
    				textfield.value="";
    		}, false);
    	textfield.addEventListener("keyup", keyuphandler, false);
    	textfield.addEventListener("keypress", keypresshandler, false);
    	textfield.addEventListener("blur",
    		function() {
    			removeErrorMessage();
    			if(this.value.replace(/\s/g,"") == "")
    				outOfField();
    		},false);
    }
    is it just a case of taking all lines like this:
    Code:
    textfield.addEventListener("keyup", keyuphandler, false);
    and changing them to:
    Code:
    if( textfield.addEventListener )
    	textfield.addEventListener("keyup", keyuphandler, false);
    else if( textfield.attachEvent )
    	textfield.attachEvent("onkeyup", keyuphandler);
    else
    	textfield.onload = keyuphandler;
    also, slightly related, i'm confused by a problem which occured a number of times to me while i was doing this: in firefox at least (v2 mac), safari as well i think, for a number of things, can't remember exactly which things now[*], using
    whatever.addEventListener("keyup",keyuphandler,false) kind of lines just didn't work and using whatever.onkeyup=keyuphandler kind of lines fixed the problem (i'm not talking about onkeyup necessarily, that's just an example event). what's going on there? how come ff which obviously supports the addEventListener function basically doesn't support it fully? how do i know when i should not use addEventListener even though if(whatever.addEventListener) returns posative, and should just use whatever.ontheevent=... instead? in fact why not just use whatever.ontheevent=... throughout from the outset -- seems to be better/safer?

    thanks.
    [*] after posting i noticed one of those things is in the snippet of code i've posted:
    Code:
    document.forms.howfar.onsubmit = howFarSubmission;

  2. #2
    We're from teh basements.
    Join Date
    Apr 2007
    Posts
    1,205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How are you calling this function in your application? Does it get called repeatedly on the same element? If you set an event listener on an object that already has a listener set for that event, it will not work.

  3. #3
    SitePoint Wizard
    Join Date
    Apr 2002
    Posts
    2,307
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    the setHowFarFormUp() function is being called from an onload handler somtimes, depending on what's being currently displayed, like this:
    Code:
    if(document.forms.howfar) setHowFarFormUp();
    else if(document.forms.newpc) setNewPCFormUp();
    also once a submission from one of the two forms has been displayed, depending on which form is then served by the ajax request and displayed as a result of that form submission, setHowFarFormUp() can be called; if the how far form is the ajax result.

    but whether it gets called more than once or not isn't the fundamental issue of whether addEventListener works or not because when i first came accross the problem it was definitely only being called once. not using addEventListener and using the ...event=... way of setting the event was the solution provided, thanks pmw57. see original thread about this: http://www.sitepoint.com/forums/showthread.php?t=569003 (the code linked to has changed/been reorganised significantly so probably isn't so relevent to that question anymore)

    also using the ..event=... way instead of the addEventListener was the solution to this thread http://www.sitepoint.com/forums/showthread.php?t=570340 . the point is using ..event=... works and addEventListener doesn't. you're saying it's when it's already been set once, then addEventListener doesn't work. but ..event=... does. in the thread i just linked to the issue was the text field being set by the user by typing something in to it. in the first thread i linked to the event had not already been set i don't think.

    the full code the above function is from is here: http://vm.ooh.uk.com/howfar/test.js
    the web page that uses it: http://vm.ooh.uk.com/howfar/testy.php
    a correct postcode to use in it: L99LL (the actual distance part isn't hooked up yet but that part is irrelevent to the js)





    what about the main first question?: to make the example code in my first question above IE compatible is this what needs doing?: all the lines like these:
    Code:
    textfield.addEventListener("keyup", keyuphandler, false);
    changing to?:
    Code:
    if( textfield.addEventListener )
    	textfield.addEventListener("keyup", keyuphandler, false);
    else if( textfield.attachEvent )
    	textfield.attachEvent("onkeyup", keyuphandler);
    else
    	textfield.onload = keyuphandler;
    is that basically it?

    thanks.
    Last edited by johnyboy; Sep 15, 2008 at 10:29.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •