Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    Feb 2006
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    object detection

    PHP Code:
    function doDOMStuff() {
    var 
    el document.getElementById('myElId'); 
    // object detection
    if(!document.getElementById) return false

    //or 

    if(!document.getElementById) return; 

    // doStuff

    Last edited by C.O.D.E.N.A.M.E; 03-22-2006 at 11:29 PM.

  • #2
    Senior Coder
    Join Date
    Feb 2006
    Location
    USA
    Posts
    1,013
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You should use the object detection before you try to use the object.

    Whether you return nothing or false is up to you. I generally return nothing to keep my code a little shorter.

    One reason to return false or something else would be to tell the code that called the function that something went wrong.
    Learn CSS. | SSI | PHP includes | X/HTML Validator | CSS validator | Dynamic Site Solutions
    Java != JavaScript && JScript != JavaScript
    Design/program for Firefox (and/or Opera), apply fixes for IE, not the other way around.

  • #3
    New Coder
    Join Date
    Feb 2006
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts
    u prefer:
    PHP Code:
    function doDOMStuff() {
    if(!
    document.getElementById) return;
    var 
    el document.getElementById('myElId'); 

  • #4
    Senior Coder
    Join Date
    Feb 2006
    Location
    USA
    Posts
    1,013
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Correct.

    Though I would use something resembling proper indentation. (I like 2 space indetantion; other people seem to prefer 3 or more.)
    Code:
    function doDOMStuff() {
      if(!document.getElementById) return;
      var el = document.getElementById('myElId');
    Learn CSS. | SSI | PHP includes | X/HTML Validator | CSS validator | Dynamic Site Solutions
    Java != JavaScript && JScript != JavaScript
    Design/program for Firefox (and/or Opera), apply fixes for IE, not the other way around.

  • #5
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    which might be the probablity to find a NS4 or an IE4 these days? Does it worth to use if(!document.getElementById) return; on and on, in each function, in each code...hoping that there might be an old Dynosaurus using those old browsers? And giving him nothing but a return ?
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #6
    New Coder
    Join Date
    Feb 2006
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts
    it is not the same level as using document.all or document.layers

    It is just a coding esthetic.


  •  

    Posting Permissions

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