Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Editing a image slideshow script - array into body 1

Status
Not open for further replies.

Zhris

Programmer
Aug 5, 2008
254
GB
Hello,

I am trying to edit an image slideshow script I found on DynamicDrive.com ( Basically, I am trying to move the array containing the images out of the main script, and put it into the body. This is because the image urls are pulled in by a Perl script after the header and first part of the body has been printed.

I have nearly achieved this by removing the array from the main script, removing the body onload event and including the following script within the body directly above the form:

Code:
<script language="JavaScript">
var Slides = new Array('myimage1.jpg','myimage2.jpg','myimage3.jpg');
window.onload = StartSlideShow();
</script>

However this change has made the script buggy i.e. when loaded, the first image is blank.

How would I go about achieving the task of moving the declaration of the array "Slides" into the body, without breaking the slideshow. Or would this be an inpractical solution.

Thank you,

Chris
 
Hi

JavaScript:
[gray]// assign to onload the return value of the function[/gray]
[gray]// unless the function returns another function, this never works[/gray]
[gray]// ( StartSlideShow() return undefined )[/gray]
window[teal].[/teal]onload [teal]=[/teal] [COLOR=darkgoldenrod]StartSlideShow[/color][teal]();[/teal]

[gray]// assign to onload a reference to the function[/gray]
window[teal].[/teal]onload [teal]=[/teal] StartSlideShow[teal];[/teal]


Feherke.
 
Feherke,

Works perfectly. Thank you very much.

Chris
 
Hi

One more thing. I would put the [tt]window.onload = StartSlideShow;[/tt] line into the slideshow script file. So if the script file is not loaded for some reason, the browser will not try to call the StartSlideShow() function.

Still one more thing. Assigning to [tt]onload[/tt] is deprecated. if you use more scripts which registers their initializer functions that way, they will overwrite each others assignment. The [tt]addEventLister()[/tt] ( standard ) and [tt]attachEvent()[/tt] ( Explorer only ) methods should be used instead. I would think again about using that script.


Feherke.
 
Hi,

I have repositioned the window.onload into the script file. I made the assumption that this wouldn't have worked because the array wouldn't have been declared yet. Thanks for that suggestion.

I figured the script must be pretty old when I first read "<script language="JavaScript">", seeing as I don't think the language attribute is used often anymore. I liked the simplicity of this script.

I need to look further into the addEventListener method. I have tried replacing the window.onload with "document.window.addEventListener("load", StartSlideShow, false);" with no luck. Is this along the right track?

Chris
 
Hi

Chris said:
document.window.addEventListener("load", StartSlideShow, false);
The [tt]document[/tt] has no window property. ( The [tt]window[/tt] object has [tt]document[/tt] property. )
Code:
window[teal].[/teal][COLOR=darkgoldenrod]addEventListener[/color][teal]([/teal][green][i]"load"[/i][/green][teal],[/teal] StartSlideShow[teal],[/teal] [b]false[/b][teal]);[/teal]

Feherke.
 
Ah your right. It wouldn't make sense for the document to have the window property. Thanks again for all your help and suggestions, much appreciated.

Chris
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top