SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Enthusiast
    Join Date
    Nov 2005
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Problem with Javascript and CSS: 'Could not convert undefined or null to object'

    Hey I just started javascript recently and I keep getting this error in the Opera javascript console:

    "Could not convert undefined or null to object"

    Here's the code:

    Code:
    function notrans() {
    
    var agt = navigator.userAgent.toLowerCase();
    var is_opera = (agt.indexOf("opera") != -1);
    
    if(is_opera) {
    document.getElementById("secondary").style.background="#000000";
    }
    }
    And here's the line from the javascript console:

    Code:
    (document.getElementById("secondary")).style.background = "#000000";
    "secondary" by the way is an ID in the external CSS document.

    So yeah, obviously the code is supposed to detect if the browser is Opera and then change the background from transparent to black....cause Opera doesn't like transparent backgrounds and it seems that the getElementById statement is the problem cause of the extra brackets and everything, but I've used similar syntex in other areas and no problem. Also I took the sniffer part from the 'Ultimate Sniffer engine on the net'.

    I think it has to do with somehow I've called a variable that hasn't been created yet, but I've tried to get around this, placing the code in external js files and also tried placing it in the head, but no good. If anyone can see what the problem is any help would be appreciated.

    This is driving me nuts...

  2. #2
    CSS & JS/DOM Adept bronze trophy
    Join Date
    Mar 2005
    Location
    USA
    Posts
    5,482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Which versions of Opera have trouble with transparent backgrounds?

    You probably need to call notrans() form window.onload.

    Using navigator.userAgent is discouraged. I recommend using window.opera instead.

    Code:
    if(window.opera) {
    P.S. It's probably just a typo but you mean "syntax", not "syntex".
    We miss you, Dan Schulz.
    Learn CSS. | X/HTML Validator | CSS validator
    Dynamic Site Solutions
    Code for Firefox, Chrome, Safari, & Opera, then add fixes for IE, not vice versa.

  3. #3
    SitePoint Enthusiast
    Join Date
    Nov 2005
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok wow... so just change the whole thing.

    So the problem is the actual elements not calling of the variable. I'll have to give this a try thanx.

  4. #4
    SitePoint Enthusiast
    Join Date
    Nov 2005
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok so i tried the window.onload thing and I got nothing, but i may have implemented this wrong.

    So then I tried the original calling of the function with window.opera and it came up with the same error. So this is the code right now:

    In external js file:

    Code:
    function notrans() {
    	if(window.opera) {
    	document.getElementById("secondary").style.background="#000000";
    }
    }
    and this is what is in the document right before the body tag but not in the head:

    Code:
    <script type="text/javascript">
    notrans();
    </script>
    and I'm still getting the same error:

    Statement on line 17: Could not convert undefined or null to object
    Backtrace:

    (document.getElementById("secondary")).style.background = "#000000";
    so maybe I did not understand properly before. This is with Opera 8.5 FYI.

    If anyone could help or just put more detail to the above post that would be great.

    Thanx in advance.

  5. #5
    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)
    You are calling the function before the page has loaded. Out the call into the onload as was earlier suggested.
    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="^$">

  6. #6
    SitePoint Enthusiast
    Join Date
    Nov 2005
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OMG I finally got it working...yeah I forgot a bracket after the function call with the window.onload. Thanks so much for the help!


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
  •