Scan your PC for Errors

Member Panel


Sponsors and Ads

Noticeboard

PC Forum PC Help Forum » Web & Networking » Web Design » Deferring javascript execution

Web Design - Deferring javascript execution posted in the Web & Networking forums; Hello there! I`m having trouble with some javascript and I think somebody here will be able to help :-D I'll give as much detail as i can. My goal is ...

JOIN US NOW to remove these Ads

Post New Thread  Reply
  #1  
Old 09-05-2008
Silver Member
My PC
 
Join Date: Sep 2006
Posts: 172
mintedjo - See this Members User comments on their Profile page
Default Deferring javascript execution

Hello there!
I`m having trouble with some javascript and I think somebody here will be able to help :-D I'll give as much detail as i can.

My goal is to add a new dom element, specifically a div element to the body tag.
The code I write works fine in firefox but in ie i get the fairly common:

"internet explorer cannot open the internet site 'www.mysite.joe'
operation aborted"

I've read up and found that the most common cause (and i think the cause of my problem) is attempting to modify the dom before it has fully loaded. However I can't fnd a method of preventing the execution of the javascript that works. I've tried placing inline code in several different positions around the page, and also in an external file. I've tried implementing some of the things I've read about but I think I'm misunderstanding something fundamental which is making this extremely difficult to solve.
Here is the code which breaks my web page:

Code:
var addADiv = true;
var myDiv;
if(addADiv){
    myDiv = document.createElement('div');
    myDiv.setAttribute('id','myNewDiv');
    myDiv.innerHTML = '<p>This is a new div element</p>';
    document.body.appendChild(myDiv);
}
As i said Firefox is fine and also if i switch addADiv to false then the rest of the code executes without any problems at all. Anyways I'm going off track a little... The thing I think I need to do is stop the javascript from executing until the dom is fully loaded. I've tried a couple of simple methods that I found on blogs but dont remember what they were now and obviously they didnt work. If you have any solutions please post as many as you can so i can try them all out, even if its a horrible dirty chunk of code. The main thing is that I get a solution :-D

One last thing; I would prefer to avoid using Mootools if possible :-)

Thanks for reading
Joe

edit: Some of the above is wrong. Whether addADiv is true or false ie still gives the error. I commented out the code and then the page loaded in ie but the js still bugged. I added 'defer="defer"' to the script tag containing the code i posted and now as long as addADiv is false the page loads without any issues and all the javascript is executed as it should be. However this brings me back to what I mentioned above which is as soon as i set it to true internet explorer has a hissy fit and brings up that crazy message. This leads me to believe that defer="defer" doesn't actually defer the script for as long as i would like it to, that is of course presuming the cause of the issue is indeed the attempt to modify the dom before its finished loading...
Ok.. if you can follow that rambling and you've got this far thanks <3


__________________
teh 1337 j03

Last edited by mintedjo; 09-05-2008 at 05:01 PM. Reason: typo
  #2  
Old 09-06-2008
Not here.
 
Join Date: Sep 2005
Posts: 1,488
PC Experience: N/A
DarkLord7854 - See this Members User comments on their Profile page DarkLord7854 - See this Members User comments on their Profile page DarkLord7854 - See this Members User comments on their Profile page DarkLord7854 - See this Members User comments on their Profile page DarkLord7854 - See this Members User comments on their Profile page DarkLord7854 - See this Members User comments on their Profile page DarkLord7854 - See this Members User comments on their Profile page DarkLord7854 - See this Members User comments on their Profile page DarkLord7854 - See this Members User comments on their Profile page DarkLord7854 - See this Members User comments on their Profile page DarkLord7854 - See this Members User comments on their Profile page
Default Re: Deferring javascript execution

Typically, when I need my JS to fire after the window loads, I use OnLoad(), or I do a state call function that waits until the status changes, onreadystatechanges() you use it in combination with status I believe.

I'll try and dig up some of my code where I've done this



Reply
Satellite TV on your PC - over 3000 Channels! Click Here!

Bookmarks

Tags
deferring , execution , javascript

Thread Tools
Display Modes

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 On

All times are GMT +1. The time now is 01:19 PM.
Powered by vBulletin
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0 RC7
All Graphics & Content Copyright © 2004-2008 - PC Help Forum.com


Back to Top