www.webdeveloper.com
Results 1 to 5 of 5

Thread: getElementById() returns null

  1. #1
    Join Date
    Apr 2009
    Location
    London, UK
    Posts
    3

    getElementById() returns null

    I got a problem with getElementById().

    Using it the way it's used here
    http://www.w3schools.com/js/tryit.as...cument_getbyid
    works fine (i.e. getting an element with a script defined on the same page as the element it's looking for).

    But if I try to call it from e.g. a FireFox extension plug-in to get hold of an element on a page that I didn't write myself, then it always returns null.

    Here's another example:
    If I use the DOM Inspector to look up the id of an element on a random page and then use the Execute JS to execute this simple code
    Code:
    alert(document.getElementById('theId'));
    were theId is the id I looked up and executed in the context of the page with that id, it always returns null. Still, calling
    Code:
    alert(document.title);
    gives the title of that page!

    What is it I don't get?
    Last edited by .:Meero:.; 04-15-2009 at 09:35 AM. Reason: typo

  2. #2
    Join Date
    May 2006
    Location
    Odenton, MD
    Posts
    1,449
    installed the add-ons and worked for me. however, make sure that the checkbox "Content Window" is marked. javascript in the add-ons execute on a different scope context, by checking the checkbox the scope is moved to window object of the target window. if you still getting null probably the element doesn't exists in target document.
    Last edited by ZeroKilled; 05-08-2009 at 11:20 PM.

  3. #3
    Join Date
    Apr 2009
    Location
    London, UK
    Posts
    3
    Thanks ZeroKiller!
    Thought I tried checking "Content Window", but apparently not.

    Still, got the same problem with my FireFox extension script. I assume there's some call I need to make that has the same effect as checking the "Content Window"?

  4. #4
    Join Date
    May 2006
    Location
    Odenton, MD
    Posts
    1,449
    doubt it, i typed directly document.getElementById and returned me correct element. however, not sure but guess this is the similar effect as if Content Window were marked:
    getBrowser().contentWindow.document.getElementById()

  5. #5
    Join Date
    Apr 2009
    Location
    London, UK
    Posts
    3
    Aha! That seems to do the trick.
    Calling this
    Code:
    var e = getBrowser().contentWindow.document.getElementById('posts');
    alert(e.innerHTML);
    now gives a nice HTML dump of the "posts" DIV element of this forum page.

    Thanks!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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



Recent Articles