0

On my current workspace site, ( run from cloud 9 IDE )I have two very simple blocks of code:

HTML

<span class="subject"> Github </span>
<div class="content">
    <hr>
    <span class="lSubj" style="color: navy;"> <a href="link"> Link </a> </span>
    <pre><div class="gitContent">Filler Text</div></pre>
</div>

Javascript:

function gitFire(){
    var gits = document.getElementsByClassName("gitContent");
    for( var i = 0; i < gits.length; i ++ )
    {
        var element = gits[i];
        alert( element.innerHTML );
    }
}

document.onload += gitFire();

CLOUD 9 space Link: https://c9.io/lemony_andrew/leandrew/workspace/test.html

Js Fiddle Link: http://jsfiddle.net/T3jkp/1/

  1. No errors in debug console.
  2. Cloud 9 isn't finding classes, but jsfiddle does.
  3. Cloud 9 Is executing the code. I believe the problem is with: getElementsByClassName
  4. Changing document.onload += gitFire; to document.onload = gitFire; does not effect the outcome.

I think it may be cloud 9's fault, but I have never had any problems like this before. Why else could it possibly work on jsfiddle and not my workspace?

Andrew
  • 3,393
  • 4
  • 25
  • 43

1 Answers1

0

You execute gitFire immediately instead of attaching a onload handler. It works on JSFiddle because they are always waiting for the DOM to be loaded before executing scripts.

document.onload = gitFire;
jgillich
  • 71,459
  • 6
  • 57
  • 85
  • `document.onload += gitFire();` and `document.onload = gitFire;` do exactly the same thing. ( at least in the outcome ). Thanks for your input. – Andrew Jul 22 '14 at 18:05
  • Wait!! Explain yourself a little more please, I just tried something in relation to your answer: `window.onload = function(){` made the script work.. `window.onload = function(){` but I changed it to work like this: `window.onload = function(){ window.onload + gitFire(); };` NOW, this works only using window. Why not document? – Andrew Jul 22 '14 at 18:14
  • I have no clue, document.onload doesn't get executed in Firefox. The best solution however is `document.addEventListener("DOMContentLoaded", gitFire);`; it works in all modern browsers. – jgillich Jul 22 '14 at 18:22
  • Hey! That worked even better, although it doesn't explain what's going on, it's a solution. You should change your answer to that and I'll except it! Thank you. – Andrew Jul 22 '14 at 18:25