Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 07-27-2010, 07:54 PM   PM User | #1
Tony M
Regular Coder

 
Join Date: Jun 2010
Posts: 132
Thanks: 8
Thanked 0 Times in 0 Posts
Tony M is on a distinguished road
Question Script Load

I want this script to load after the page load
the script is:
Code:
<script type="text/javascript" src="http://localtimes.info/clock.php?cp3_Hex=0F0200&cp2_Hex=FFFFFF&cp1_Hex=000080&fwdt=118&ham=0&hbg=0&hfg=0&sid=&mon=&wek=&wkf=&sep=&continent=Asia&country=Indonesia&city=Jakarta&widget_number=116"></script>
the script in My page is like:
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>My test page</title>
</head>

<body>
<h1>Tony</h1>
<h2>Welcome Tony Website</h2>
<script type="text/javascript" src="http://localtimes.info/clock.php?cp3_Hex=0F0200&cp2_Hex=FFFFFF&cp1_Hex=000080&fwdt=118&ham=0&hbg=0&hfg=0&sid=&mon=&wek=&wkf=&sep=&continent=Asia&country=Indonesia&city=Jakarta&widget_number=116"></script>
<h3>Tony is ... and is....</h3>
<h3>He will....</h3>
</body>

</html>
So how can I make this script loads after all elements of My page are loaded and without changing it's position (the script position)?
Tony M is offline   Reply With Quote
Old 07-27-2010, 09:08 PM   PM User | #2
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,033
Thanks: 197
Thanked 2,410 Times in 2,388 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
You can only load the script last of all by placing it right ahead of the </body> tag.

But you can load the script and delay the execution of the function(s) contained within it by

window.onload = function() {
functionOne();
functionTwo();
}

As your script is loaded from an external source this may not be posssible.

Quizmaster: How many centimeters are there in a metre?
Contestant: Two and a half.

Last edited by Philip M; 07-27-2010 at 09:11 PM..
Philip M is offline   Reply With Quote
Old 07-27-2010, 10:11 PM   PM User | #3
RandomUser531
New Coder

 
Join Date: Jul 2010
Posts: 61
Thanks: 0
Thanked 21 Times in 21 Posts
RandomUser531 is on a distinguished road
Try this. Anything else using onload must be combined with it.
Code:
<h2 id='myElem'>Welcome Tony Website</h2>

............

<script type="text/javascript">

window.onload = function()
{
  var myScript = document.createElement( 'script' ), 
      elem = document.getElementById( 'myElem' );

  myScript.src = "http://localtimes.info/clock.php?cp3_Hex=0F0200&cp2_Hex=FFFFFF&cp1_Hex=000080&fwdt=118&ham=0&hbg=0&hfg=0&sid=&mon=&wek=&wkf=&sep=&continent=Asia&country=Indonesia&city=Jakarta&widget_number=116";

  elem.parentNode.insertBefore( myScript, elem.nextSibling );
}
</script>
RandomUser531 is offline   Reply With Quote
Old 07-28-2010, 07:48 AM   PM User | #4
Tony M
Regular Coder

 
Join Date: Jun 2010
Posts: 132
Thanks: 8
Thanked 0 Times in 0 Posts
Tony M is on a distinguished road
Question

ok, how to put these codes in My page?

Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>My test page</title>
</head>

<body>
<h1>Tony</h1>
<h2>Welcome Tony Website</h2>
<script type="text/javascript" src="http://localtimes.info/clock.php?cp3_Hex=0F0200&cp2_Hex=FFFFFF&cp1_Hex=000080&fwdt=118&ham=0&hbg=0&hfg=0&sid=&mon=&wek=&wkf=&sep=&continent=Asia&country=Indonesia&city=Jakarta&widget_number=116"></script>
<h3>Tony is ... and is....</h3>
<h3>He will....</h3>
</body>

</html>
Tony M is offline   Reply With Quote
Old 07-28-2010, 01:02 PM   PM User | #5
RandomUser531
New Coder

 
Join Date: Jul 2010
Posts: 61
Thanks: 0
Thanked 21 Times in 21 Posts
RandomUser531 is on a distinguished road
Quote:
Originally Posted by Tony M View Post
ok, how to put these codes in My page?
You can put the <script> block pretty much anywhere since it does not execute until the document has finished loading, at which time it will insert your specified script immediately after the element with id='myElem'.
Presumably your script is suitable for insertion into a loaded document.
RandomUser531 is offline   Reply With Quote
Old 07-28-2010, 06:29 PM   PM User | #6
Tony M
Regular Coder

 
Join Date: Jun 2010
Posts: 132
Thanks: 8
Thanked 0 Times in 0 Posts
Tony M is on a distinguished road
The code that You showed Me above is not working, please help?

Last edited by Tony M; 07-28-2010 at 07:59 PM..
Tony M is offline   Reply With Quote
Old 07-28-2010, 10:51 PM   PM User | #7
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,168
Thanks: 59
Thanked 3,993 Times in 3,962 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Phil & Philip: I think you are misdiagnosing his problem.

That script he is invoking is used to document.write code to embed a Flash object in the page.

Just point your browser to that url:
http://localtimes.info/clock.php?cp3...get_number=116
to see the JS code.

So he *CAN'T* delay it until page load. Period. He *MUST* create the Flash object inline.

*IF* he had access to the code, he could change it so that the Flash movie doesn't start until requested, but that's about all he could do.

He could also dump out what the code is generating and, instead of using that code just embed the Flash by hand and than change when the movie starts.

But he can't do it by simply using onload to delay when the code is invoked.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 07-29-2010, 12:21 AM   PM User | #8
RandomUser531
New Coder

 
Join Date: Jul 2010
Posts: 61
Thanks: 0
Thanked 21 Times in 21 Posts
RandomUser531 is on a distinguished road
Quote:
Originally Posted by Old Pedant View Post
Phil & Philip: I think you are misdiagnosing his problem.

That script he is invoking is used to document.write code to embed a Flash object in the page.
Well I did warn about that possibility, but now I know there's only one thing for it.

1) Remove the code I gave you.

2) Place your original <script> tags for the clock at the end of the <body> section, then immediately below those tags (still inside the body) add this script block:
Code:
<script type='text/javascript'>

(function()
{
 var objs = document.getElementsByTagName('object'), 
     me = document.getElementById( 'myElem' );  
   
 for( var i = 0, found = false, clsId; i < objs.length && !found; i++ )
 {
  clsId = objs[i].getAttribute('classid') || "";
      
  if( clsId === "clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" )
  {
   found = true;  
   me.parentNode.insertBefore( objs[i], me.nextSibling );
  }
 } 
})();

</script>
3) Make sure this element has the ID as shown here: <h2 id='myElem'>Welcome Tony Website</h2>
RandomUser531 is offline   Reply With Quote
Users who have thanked RandomUser531 for this post:
Tony M (07-29-2010)
Old 07-29-2010, 01:25 AM   PM User | #9
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,168
Thanks: 59
Thanked 3,993 Times in 3,962 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Doesn't he also need to remove the objs[i] object?? Or does the insertBefore just move it, automatically, because it can't be in two places at once?

And what's the purpose of the found flag?
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 07-29-2010, 03:04 AM   PM User | #10
RandomUser531
New Coder

 
Join Date: Jul 2010
Posts: 61
Thanks: 0
Thanked 21 Times in 21 Posts
RandomUser531 is on a distinguished road
Quote:
Originally Posted by Old Pedant View Post
Doesn't he also need to remove the objs[i] object?? Or does the insertBefore just move it, automatically, because it can't be in two places at once?
Yes it's automatic, although Opera used to be a bit shaky about it.
Quote:
And what's the purpose of the found flag?
To end the loop prematurely in an overt manner if further iterations are redundant.
RandomUser531 is offline   Reply With Quote
Old 07-29-2010, 02:23 PM   PM User | #11
Tony M
Regular Coder

 
Join Date: Jun 2010
Posts: 132
Thanks: 8
Thanked 0 Times in 0 Posts
Tony M is on a distinguished road
Question

ok, finaly the script is working,
BUT it is working fine only on Internet Explorer, but not on Firefox, on Firefox the clock is Hovering above the text that is supposed to be after it, and not under it? So what is the solution?

Last edited by Tony M; 07-29-2010 at 02:52 PM..
Tony M is offline   Reply With Quote
Old 07-29-2010, 03:46 PM   PM User | #12
RandomUser531
New Coder

 
Join Date: Jul 2010
Posts: 61
Thanks: 0
Thanked 21 Times in 21 Posts
RandomUser531 is on a distinguished road
Quote:
Originally Posted by Tony M View Post
ok, finaly the script is working,
BUT it is working fine only on Internet Explorer, but not on Firefox, on Firefox the clock is Hovering above the text that is supposed to be after it, and not under it? So what is the solution?
That will just be a page-layout issue. Try adding a <p> or <br> tag after your <h2> and transfer id='myElem' to that tag. Whatever you do, remember that the clock will be inserted immediately after whichever element has id='myElem'.
RandomUser531 is offline   Reply With Quote
Users who have thanked RandomUser531 for this post:
Tony M (07-29-2010)
Old 07-29-2010, 04:49 PM   PM User | #13
Tony M
Regular Coder

 
Join Date: Jun 2010
Posts: 132
Thanks: 8
Thanked 0 Times in 0 Posts
Tony M is on a distinguished road
Question

Ok, the script is working, thanks.
I had those last 2 questions:

1- If I put the srcipt (that You showed Me above)
Code:
<script type='text/javascript'>

(function()
{
 var objs = document.getElementsByTagName('object'), 
     me = document.getElementById( 'myElem' );  
   
 for( var i = 0, found = false, clsId; i < objs.length && !found; i++ )
 {
  clsId = objs[i].getAttribute('classid') || "";
      
  if( clsId === "clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" )
  {
   found = true;  
   me.parentNode.insertBefore( objs[i], me.nextSibling );
  }
 } 
})();

</script>
does it make all the JavaScript scripts in My page to load after the loading time?
(for example I had a second clock but the script of the other clock isn't directly over the script that You showed Me, so does this other clock load after the page loads, or it will load normally with the page according to it's position)

2- If I put a Javascript code that shows that the page is loading "Please Wait", does the clock code (that We discussed in the posts above), will be loaded before or after the loading message (please wait) disappears?

Last edited by Tony M; 07-29-2010 at 04:53 PM..
Tony M is offline   Reply With Quote
Reply

Bookmarks

Tags
loading, script

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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

Forum Jump


All times are GMT +1. The time now is 12:56 AM.


Advertisement
Log in to turn off these ads.