File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes HTML, CSS and JavaScript and the fly likes serious problem about script loading Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "serious problem about script loading" Watch "serious problem about script loading" New topic
Author

serious problem about script loading

Goran Markovic
Ranch Hand

Joined: Sep 26, 2008
Posts: 399
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

Joined: Jan 10, 2002
Posts: 61451
    
  67

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Goran Markovic
Ranch Hand

Joined: Sep 26, 2008
Posts: 399
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

Joined: Jan 10, 2002
Posts: 61451
    
  67

Without jQuery's ready handler, you'd make use of the the onload handler.
Goran Markovic
Ranch Hand

Joined: Sep 26, 2008
Posts: 399
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

Joined: Jan 10, 2002
Posts: 61451
    
  67

Correct.
Goran Markovic
Ranch Hand

Joined: Sep 26, 2008
Posts: 399
ok. thanks, until next 'serious' or trivial problem
thanks again.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: serious problem about script loading