This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

serious problem about script loading

 
Goran Markovic
Ranch Hand
Posts: 399
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am starting to believe that some virus infected my PC.
Recently I have a post about problem during loading an external file, but I have also the same problem in 'in-document' scripts.
The script won't be loaded!!! However, it 'load' script, but have no access to any element in the document, unless I specified a scripts elements right before body closing tag.
However, that's happened only with a files made by myself. If i try some exercise from the book example (where script tag or script importing occurs right in the head element) everything works just fine. Do you get it???
I would introduce the test example and explanation of its behavior, tested in IE, Opera, Firefox, Safari.
script inside head element

Won't work in neither browser, no events occurs on click, and IE report the error div is null or not an object
Next, script is put right after opening body tag :


Won't work in neither browser, no events occurs on click, and IE report the error div is null or not an object.
Next, put scripts right before closing body element.

Works fine in each browser!
I am really on the edge of my nerves, so please, does anyone has an explanation for my problem???
Thanks you....
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65218
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Of course that won't work. A script placed in-line within the head is executed before the DOM tree is even created, so of course it's not going to find any element.

You can't reference an element before it is created.

That's why the onload handler, and if you are using jQuery, the ready handler, exist. These handlers will be called after the DOM tree has been created.

P.S. Starting off a post with "I think I have a virus" is a good way to get a reputation as a boy who cries wolf.
 
Goran Markovic
Ranch Hand
Posts: 399
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Bear. My intention was not certainly to make some reputation of mine.Recently I have a strange behavior of my PC, due to virus, so I just have been suspicious (at least 1 hour re-installation of my system is the most worse scenario).
About my question, I figure out that objects were not read, the DOM tree isn't created when my js are invoked, but I was confused little. I resolved any doubt's by using JQuery, something like this :


But, is that mean that, using pure JavaScript, I cannot perform putting of js into ,head/. element like this.

 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65218
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Without jQuery's ready handler, you'd make use of the the onload handler.
 
Goran Markovic
Ranch Hand
Posts: 399
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Bear. As much as conclude, use of JQuery is greater as it have a capability to act on the DOM elements, right after DOM configuration ( I do not need to wait entire page content to be loaded.images, etc.), where, when using onload handler, entire page must be rendered (including entire content), before I can act on the DOM. Right?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65218
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Correct.
 
Goran Markovic
Ranch Hand
Posts: 399
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok. thanks, until next 'serious' or trivial problem
thanks again.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic