Current location: Hot Scripts Forums » General Web Coding » JavaScript » onload issue


onload issue

Reply
  #1 (permalink)  
Old 03-26-09, 10:58 AM
kelkadir kelkadir is offline
Newbie Coder
 
Join Date: Dec 2005
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
onload issue

Hi everybody.

the onload event handler is used to "invoke JavaScript after the page or an image has finished loading."

I need an event handler or an idea on how to "invoke JavaScript as soon as the page or an image starts loading." The event should stop once the page or image has stopped loading

Thanks in advance for your suggestions

Simon

Last edited by kelkadir; 03-26-09 at 11:00 AM.
Reply With Quote
  #2 (permalink)  
Old 03-27-09, 04:06 AM
Shto Shto is offline
Newbie Coder
 
Join Date: Aug 2008
Location: Eindhoven, NL
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
Hi Simon.

I am not sure if that is possible. Then again, maybe if you tell us in more detail over what you want to do, we could help more.

What I think you can do is: before the image tags make some dummy empty span tags and just attach an onload event to those. Then, when the span tag completes loading, the event would be triggered before the image tag is loaded.

About the page, you could just put a <script></script> area before the <body> tag. Since the page loads from upper code lines to lower code lines, it should go through the javascript before it reaches the <body> tag.

Hope this helps.
Reply With Quote
  #3 (permalink)  
Old 03-28-09, 09:09 AM
de.monkeyz's Avatar
de.monkeyz de.monkeyz is offline
Wannabe Coder
 
Join Date: Apr 2008
Location: Leeds, UK
Posts: 116
Thanks: 0
Thanked 0 Times in 0 Posts
Your description of what you want technically isnt an event. As you said it would stop when its loaded. But events only fire the once, not over and over until something else happens.

If you wanted something to occur every so often until the page is loaded you could make an interval for a function, and then remove it on the image or page load.
__________________
Wanna learn AJAX? Goto http://www.deathmonkeyz.com/tutorials for free video tutorials.

AJAX - Lesson 11 - AJAX Guestbook (23/8/08)
C++ - Lesson 10 - Classes (24/8/08)
JavaScript - Lesson 03 - The DOM (24/8/08)
Need an AJAX app? Look no further, I'm available for work
Reply With Quote
  #4 (permalink)  
Old 03-28-09, 10:23 AM
wirehopper's Avatar
wirehopper wirehopper is offline
-
 
Join Date: Feb 2006
Posts: 2,515
Thanks: 20
Thanked 109 Times in 106 Posts
Code:
<html>
<head>
<script type="text/javascript">
alert ('Started loading page');
function loaded(e)
{
alert('Image loaded');
}
</script>
<title>Title</title>
....
</head>
<body>
<p>
<img id="imgId" src="blank.gif" onload="loaded(this)" />
</p>
</body>
</script type="text/javascript">
alert ('Setting the src of the image');
document.getElementById('imgId').src='image.gif';
</script>
</html>
Reply With Quote
  #5 (permalink)  
Old 03-28-09, 11:18 AM
kelkadir kelkadir is offline
Newbie Coder
 
Join Date: Dec 2005
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
getting near

Thank you for the code abode. Trying it......
Reply With Quote
  #6 (permalink)  
Old 03-28-09, 11:20 AM
kelkadir kelkadir is offline
Newbie Coder
 
Join Date: Dec 2005
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Solution?

Thanks for the code above. It is getting me nearer.
Here is the link to what I would like to do

http://mayamundotours.com/test.php

The animated jpg is showing as soon as the page starts loading which is great, because this is exactly what I need. However, I would like this animated jpg to disppear as soon as the page has finished loading.

Is there a way to do the following:
"on unload (or when the page has finished loading), erase the content of document.write () so that the animated jpg stops showing"?

here is the current code,

HTML Code:
<html>
<head>
<script type="text/javascript">
document.write ('<img src="http://www.mayamundotours.com/ajax-loader.gif" border=0>');
function loaded(e)
{
alert('Image loaded');
}
</script>
<title>Title</title>
....
</head>
<body>
<p>
<img id="imgId" src="1.jpg" onload="loaded(this)" />
</p>
</body>
Thank you in advance

Simon

Last edited by UnrealEd; 03-28-09 at 11:37 AM. Reason: fixed [html] tags
Reply With Quote
  #7 (permalink)  
Old 03-28-09, 11:39 AM
UnrealEd's Avatar
UnrealEd UnrealEd is offline
Community Liaison
 
Join Date: May 2005
Location: Antwerp, Belgium
Posts: 3,165
Thanks: 4
Thanked 25 Times in 25 Posts
Have you tried hiding the image?
__________________
"Good judgement comes from experience, and experience comes from bad judgement." - Fred Brooks

Reply With Quote
  #8 (permalink)  
Old 03-28-09, 04:20 PM
kelkadir kelkadir is offline
Newbie Coder
 
Join Date: Dec 2005
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
follow up

I tried the following to hide the image, but it unfortunately doesn't work
HTML Code:
<html>
<head>
<script type="text/javascript">
document.write ('<img src="http://www.mayamundotours.com/ajax-loader.gif" id="theimage" border=0>');
function loaded(e)
{
document.getElementById(theimage).style.display = "none";
}
</script>
<title>Title</title>
....
</head>
<body>
<p>
<img id="imgId" src="1.jpg" onload="loaded(this)" />
</p>
</body>
Any suggestion are greatly appreciated

Last edited by TwoD; 03-30-09 at 01:19 PM.
Reply With Quote
  #9 (permalink)  
Old 03-28-09, 06:48 PM
kelkadir kelkadir is offline
Newbie Coder
 
Join Date: Dec 2005
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Solved

Just in case it could be of interest to the public, I solved the issue by doing this,
HTML Code:
<html>
<head>

<title>Title</title>

</head>
<body>
<p>

<img src="http://www.mayamundotours.com/ajax-loader.gif" id="theimage" border=0>

<img id="imgId" src="1.jpg" onload="loaded(this)" />
</p>
</body>

<script type="text/javascript">

function loaded(e)
{
document.getElementById('theimage').style.visibility = 'hidden';
}
</script>

</html>

I find this is a good practice to avoid that your site visitors don't just click away if your pages take long to load!

cheers
Simon

Last edited by TwoD; 03-30-09 at 01:20 PM.
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] window.onload and body onload issue scott2500uk JavaScript 2 09-21-08 08:35 AM
[SOLVED] PHP AJAX and IE disabled option issue Dan Man PHP 1 08-26-08 02:34 PM
css issue darkcarnival CSS 4 10-11-05 08:35 PM
Issue Tracking and Task management SlackeR Script Requests 8 10-05-05 04:29 PM
Language issue in from property, Please Help AskAR ASP 0 08-10-05 06:40 AM


All times are GMT -5. The time now is 11:49 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.