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!

dividing script up to import into html

Status
Not open for further replies.

JSCboulder

Technical User
Oct 6, 2008
6
DE
Here is the question:
the script below is simply taking a random image from a list and writing in on the page.

How do I divide this up so I don't have to put the whole script on the html page? In other words what can be put into a js file and imported, and how would the snippet look that actually sits in the html, calls that script and writes the image?
thanks,
JSC Boulder

<script type="text/javascript">
<!--
function random_imglink(){
var myimages=new Array()
myimages[1]="<?=_p()?>/images/groups/img1.jpg"
myimages[2]="<?=_p()?>/images/groups/img2.jpg"
myimages[3]="<?=_p()?>/images/groups/img3.jpg"
myimages[4]="<?=_p()?>/images/groups/img4.jpg"
myimages[5]="<?=_p()?>/images/groups/img5.jpg"

var ry=Math.floor(Math.random()*myimages.length)
if (ry==0)
ry=1
document.write('<a href= src="'+myimages[ry]+'" border=0 alt=groups></a>')
}
random_imglink()
//-->
</script>
 
As it stands pretty much the entire script can be placed in an external JS file and called in as it looks like you are simply calling the function immediately after declaring it.


So basically you could do this:

Code:
<script src="/path/to/js_file.js"></script>
<script type="text/javascript">random_imglink();</script>

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Behind the Web, Tips and Tricks for Web Development.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top