...

View Full Version : Calling a external script question



jdsflash
12-11-2009, 02:33 PM
I have two pages one html page and one Javascript page index.html and script.js. I want to call the javascript page 3 times to display 3 images.

How would I do this from my html page.

script.js


<script language="JavaScript">
<!--
/*
Random Image Link Script
By Website Abstraction (http://www.wsabstract.com)
and Java-scripts.net (http://www.java-scripts.net)
*/

function random_imglink(){
var myimages=new Array()
//specify random images below. You can have as many as you wish
myimages[1]="http://flashmajic.com/Google/atomica/atomicasmallicon.jpg"
myimages[2]="http://flashmajic.com/Google/atomica/atomicasmallicon.jpg"
myimages[3]="http://flashmajic.com/Google/atomica/atomicasmallicon.jpg"

//specify corresponding links below
var imagelinks=new Array()
imagelinks[1]="http://www.wsabstract.com"
imagelinks[2]="http://www.dynamicdrive.com"
imagelinks[3]="http://www.java-scripts.net"

var ry=Math.floor(Math.random()*myimages.length)

if (ry==0)
ry=1
document.write('<a href='+'"'+imagelinks[ry]+'"'+'><img src="'+myimages[ry]+'" border=2 hspace=1></a>')

}

random_imglink()
//-->
</script>


index.html


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>

?????
How would I call that script 3 times to display 3 random images?


</body>
</html>

angst
12-11-2009, 02:37 PM
just call the function 3 times,



<script>random_imglink();random_imglink();random_imglink();</script>


or



<script>random_imglink()</script>
<script>random_imglink()</script>
<script>random_imglink()</script>



also script.js doesn't need "<script language="JavaScript">" tags and can be included in your html page like:


<script type='text/javascript' src='/script.js'></script>

gusblake
12-11-2009, 02:38 PM
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>

?????
How would I call that script 3 times to display 3 random images?

like this i suppose,

<script type="text/javascript">
random_imglink();
random_imglink();
random_imglink();
</script>

or

<script type="text/javascript">
for(i=0;i<3;i++) {
random_imglink();
}
</script>

</body>
</html>


Edit - yes, what angst said

jdsflash
12-11-2009, 02:59 PM
I tried both approaches below with no results. I never used javascript before so this is kinda frustrating but ill get it. Do you see any errors?.

Here are the page links
www.flashmajic.com/test.html
www.flashmajic.com/test.js




<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>


<script type='text/javascript' src='/test.js'>

random_imglink();
random_imglink();
random_imglink();

</script>

</body>
</html>



Different attempt


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<script type="text/javascript">
random_imglink();
random_imglink();
random_imglink();
</script>






</body>
</html>

angst
12-11-2009, 03:02 PM
try this:


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>

<script type='text/javascript' src='/test.js'></script>


<script>
random_imglink();
random_imglink();
random_imglink();
</script>



</body>
</html>

jdsflash
12-11-2009, 03:23 PM
Thats not working either. Does the script need to be modified to work across two pages? IT was intended to be used in a html page.

I want it on a js files so i can call it from many locations and only update one page to work across all locations.

angst
12-11-2009, 03:25 PM
I just tested it and it works for me.

jdsflash
12-11-2009, 03:28 PM
http://www.flashmajic.com/test.html

you tested that page? what browser?

angst
12-11-2009, 03:40 PM
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>

<script type='text/javascript' src='test.js'></script>


<script>
random_imglink();
random_imglink();
random_imglink();
</script>



</body>
</html>








function random_imglink(){
var myimages=new Array()
//specify random images below. You can have as many as you wish
myimages[1]="http://flashmajic.com/Google/atomica/atomicasmallicon.jpg"
myimages[2]="http://flashmajic.com/Google/atomica/atomicasmallicon.jpg"
myimages[3]="http://flashmajic.com/Google/atomica/atomicasmallicon.jpg"

//specify corresponding links below
var imagelinks=new Array()
imagelinks[1]="http://www.wsabstract.com"
imagelinks[2]="http://www.dynamicdrive.com"
imagelinks[3]="http://www.java-scripts.net"

var ry=Math.floor(Math.random()*myimages.length)

if (ry==0)
ry=1
document.write('<a href='+'"'+imagelinks[ry]+'"'+'><img src="'+myimages[ry]+'" border=2 hspace=1></a>')

}




chrome, FF & IE works for me.

jdsflash
12-11-2009, 04:04 PM
That works perfect! Thanks alot for you time and help.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum