SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Member
    Join Date
    Jun 2005
    Posts
    23
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Javscript "...has no properties" ???

    I have the following script (sets on states):
    // set nav on states
    function setNav(id,navStyle) {
    var bVersion = parseInt(navigator.appVersion);
    var bName = navigator.appName;
    var myObj = document.getElementById(id);

    if ((bName.indexOf('Netscape')>-1 && bVersion >4) ||(bName.indexOf('Explorer')>-1 && bVersion >=4)){
    myObj.className=navStyle;
    }
    }

    function eval_location(){
    if (typeof(tkeys) != 'undefined'&& tkeys.length>1) { setNav('location',tkeys,'','navOn');
    }
    }

    // define sections
    var tkeys = new Array('overviewOn','phasesOn','newsOn','subscribeOn','galleryOn','contactOn');
    It results in an error that says "myObj has no properties". It works fine, but I get an error when I check code validity... any help would be greatly appreciated.

    Many thanks in advance,

    jonpb

  2. #2
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quite nasty browser check you have before you set the className property. Why do you use it?

    The reason, I guess, that myObj is null is becaues it can not find the element.

  3. #3
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Since you never call the function, your script should work fine.

  4. #4
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,862
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    If that statement is running before an element is added to the page with that id then the getElementById will fail. An element has to be loaded before you can access it.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  5. #5
    SitePoint Member
    Join Date
    Jun 2005
    Posts
    23
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thanks for the replies... But...

    This script was provided by a contractor (for a work project) who is no longer around. The script works fine, but I'd like to correct the error in the interest of having an 'error free, compliant" site.

    I'm no expert with JavaScript, so is there a way to resolve this issue and still have it work as intended?

    Again, Many thanks in advance for your assistance.

    Best,

    Jonpb

  6. #6
    SitePoint Guru
    Join Date
    Apr 2006
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    var myObj = document.getElementById(id);
    Below this line, test for myObj before you try to use it:

    if(!myObj) return;

  7. #7
    SitePoint Guru
    Join Date
    Apr 2006
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Moderator: please remove this (sorry- double post. )

  8. #8
    SitePoint Member
    Join Date
    Jun 2005
    Posts
    23
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by mrhoo View Post
    Below this line, test for myObj before you try to use it:

    if(!myObj) return;
    Ahhh... Thank You! This works beautifully.

    Cheers,

    Jonpb


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
  •