Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

JavaScript Forum


You are currently viewing our JavaScript Forum as a guest. Please register to participate.
Login



Reply
Why does document.getElementsByTagName("span") == null?
Old 11-18-2007, 03:01 AM Why does document.getElementsByTagName("span") == null?
ForrestCroce's Avatar
Half Man, Half Amazing

Posts: 3,023
Name: Forrest Croce
Location: Seattle, WA
Trades: 0
When I put the function call into a js debugger in FF, it shows a number of results. But at the bottom of a page, it returns null, as if the DOM hasn't been constructed yet by the time my script runs.

Example. I'm trying to read what categories a post is filed under, and then light those up in the category tree. It seems like that would help the navigation be a little more natural, and also just be cool. I've tried a number of different syntax possibilities Google suggested for script defer.

Here's the code, which runs without throwing any errors ... it just doesn't return any data, either:

Code:
<script defer="defer" type="text/javascript">
  var temp = "";
  var allSpans = document.getElementsByTagName("span");
  for(var i = 0; i < allSpans.length; i++)
     if(allSpans[i].class == "postmetadata") {
      var catLinks = allSpans.getElementsByTagName("a");
      for(var a = 0; a < catLinks.length; a++)
        temp += catLinks[a].innerText + ", ";
    }
  window.status = "Categories:  " + temp;</script>
How do I get this to run after the document tree is built so I can query the DOM? None of the italic code above ever gets a chance to run...
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE

Last edited by ForrestCroce; 11-18-2007 at 03:02 AM..
ForrestCroce is offline
Reply With Quote
View Public Profile Visit ForrestCroce's homepage!
 
 
Register now for full access!
Old 11-18-2007, 03:24 AM Re: Why does document.getElementsByTagName("span") == null?
ForrestCroce's Avatar
Half Man, Half Amazing

Posts: 3,023
Name: Forrest Croce
Location: Seattle, WA
Trades: 0
Apparently you need to put the script in an external file to make this work...?

And, for the record, it's innerHTML, not innerText.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
ForrestCroce is offline
Reply With Quote
View Public Profile Visit ForrestCroce's homepage!
 
Old 11-18-2007, 05:05 AM Re: Why does document.getElementsByTagName("span") == null?
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
Forrest, did you put your script at the top of the page ?

If so, this might be your problem.
Javascript is interpreted as the browser "sees" it.
If it's placed before HTML content, then it won't see any span, as they are not rendered at this time.

What you need is to launch your DOM parsing once the page is loaded
Code:
function initSpan(){
  var temp = "";
  var allSpans = document.getElementsByTagName("span");
  for(var i = 0; i < allSpans.length; i++)
     if(allSpans[i].class == "postmetadata") {
      var catLinks = allSpans.getElementsByTagName("a");
      for(var a = 0; a < catLinks.length; a++)
        temp += catLinks[a].innerText + ", ";
    }
  window.status = "Categories:  " + temp;
}
 
try{
  window.addEventListener('load',initSpan, true);
}
catch(err){
  window.attachEvent('onload',initSpan);
}
This makes the initSpan() function being called upon the onload event, and should resolve your problem.
It should be the same when you include js in an external file, it's maybe an coincidence if it works when you do so.
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 11-18-2007, 03:16 PM Re: Why does document.getElementsByTagName("span") == null?
ForrestCroce's Avatar
Half Man, Half Amazing

Posts: 3,023
Name: Forrest Croce
Location: Seattle, WA
Trades: 0
I had put this one at the bottom of the page, just before the analytics code and </body>, for exactly the reason you mentioned. I had noticed long ago that script doesn't "see" document elements that are declared lower down in the code. Sort of like C, unless I'm not remembering properly ... I don't think you could declare an instance of a class you hadn't defined yet?

Anyway, it's probably a coincidence, like you said, but externalizing the code seems to have fixed it. Running line by line in the debugger in FF, it would hit the .innerText property and then start the next iteration of the loop. Confusing error handling...

I've never tried attaching events in js ... I've never been that comfortable understanding exactly how they bubble in javascript.

Here's the working code, below. Have a look! Now the question is whether I want to disable this on everything but individual posts...?

Code:
    var temp = "";
    var allSpans = document.getElementsByTagName("span");

    for(var i = 0; i < allSpans.length; i++)
        if(allSpans[i].className == "postmetadata") {
            var catLinks = allSpans[i].getElementsByTagName("a");
            for(var a = 0; a < catLinks.length; a++)
                if(catLinks[a].innerHTML)
                    temp += catLinks[a].innerHTML + ", ";
        }

    // Now update the tree
    var catLinks2 = document.getElementById("categoryList").getElementsByTagName("a");
    for(var b = 0; b < catLinks2.length; b++)
        if(temp.indexOf(catLinks2[b].innerHTML) > -1) {
            catLinks2[b].style.fontWeight = "bold";
            catLinks2[b].style.fontFamily = "helvetica";
            catLinks2[b].style.color = "purple";
        }
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
ForrestCroce is offline
Reply With Quote
View Public Profile Visit ForrestCroce's homepage!
 
Reply     « Reply to Why does document.getElementsByTagName("span") == null?
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.14114 seconds with 12 queries