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 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Nov 2009
    Location
    Florida
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Error in IE8: Message: 'document.getElementById(...)' is null or not an object

    Hi, first post here. I was working on code for opening a link in a new window when clicked. I read somewhere that it's a good idea to remove all of the javascript elements from the HTML. So I pieced it together and it works. But I get the following error in IE8:

    Code:
    Message: 'document.getElementById(...)' is null or not an object
    Here's the relevant HTML:

    Code:
    <a href="http://www.samplesite.com" id="sampleid">Sample Text</a>
    And here's the relevant javascript that I put in a seperate .js file:

    Code:
    function samplefct() {
    window.open(this.href);return false;
    }
    
    window.onload = function()
    {document.getElementById("sampleid").onclick = samplefct;
    }
    The code does work as planned. But I'd still like to get rid of the error. Can anybody think of a different way of stating the javascript that would perform the same thing and lose the error?

    Any help would be greatly appreciated.
    Thanks!
    IB

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,919
    Thanks
    79
    Thanked 4,423 Times in 4,388 Posts
    Only thing I can think of is that maybe you have two objects with that same id on the page???

    Can you show it live?

    And not to ask a silly question, but why not get rid of all the JS code and just do:
    Code:
    <a href="http://www.samplesite.com" target="_new">Sample Text</a>
    ???
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    New to the CF scene
    Join Date
    Nov 2009
    Location
    Florida
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks Old Penant. I think you might be on the right track. I don't have two objects with the same ID. But I tried isolating things, creating a new HTML page and a new .js file with only the code in question. I don't get the error now in IE. So there's something on my pages interacting and causing the error. I'll just have to go thru and keep narrowing it down until I isolate the problem.

    I read about the target="new" option and thought about it. I'm not worried about it not validating with Strict XHTML. But I decided I didn't want to force a window on users that have js turned off on purpose. The 'open new window' link goes to a shipping option description page, coming from a Miva shopping cart page towards the end of the order process. That's why I have it open in a new window. Name and address info is already filled out, so I don't want the user to get lost and end up having to re-enter that info. For people that go out of their way to turn js off, I don't want to tick them off by opening a new window. Probably not a big deal. I could go either way with it really.

    Thanks for the help!
    IB

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,919
    Thanks
    79
    Thanked 4,423 Times in 4,388 Posts
    Oh, I got you. So if they have JS turned off, you just let them end up with the page in their main window instead. Makes lots of sense. Will remember that one.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

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