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 12-11-2009, 02:33 PM   PM User | #1
jdsflash
New Coder

 
Join Date: Dec 2009
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
jdsflash is an unknown quantity at this point
Calling a external script question

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
Code:
<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
Code:
<!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>
jdsflash is offline   Reply With Quote
Old 12-11-2009, 02:37 PM   PM User | #2
angst
Senior Coder

 
angst's Avatar
 
Join Date: Apr 2004
Location: Toronto, Ontario
Posts: 2,112
Thanks: 15
Thanked 122 Times in 122 Posts
angst is on a distinguished road
just call the function 3 times,

Code:
<script>random_imglink();random_imglink();random_imglink();</script>
or

Code:
<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:

Code:
<script type='text/javascript' src='/script.js'></script>
angst is offline   Reply With Quote
Old 12-11-2009, 02:38 PM   PM User | #3
gusblake
Regular Coder

 
Join Date: Jan 2006
Posts: 568
Thanks: 6
Thanked 84 Times in 84 Posts
gusblake is on a distinguished road
Code:
<!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
gusblake is offline   Reply With Quote
Old 12-11-2009, 02:59 PM   PM User | #4
jdsflash
New Coder

 
Join Date: Dec 2009
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
jdsflash is an unknown quantity at this point
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


Code:
<!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
Code:
<!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>
jdsflash is offline   Reply With Quote
Old 12-11-2009, 03:02 PM   PM User | #5
angst
Senior Coder

 
angst's Avatar
 
Join Date: Apr 2004
Location: Toronto, Ontario
Posts: 2,112
Thanks: 15
Thanked 122 Times in 122 Posts
angst is on a distinguished road
try this:
Code:
<!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>
angst is offline   Reply With Quote
Old 12-11-2009, 03:23 PM   PM User | #6
jdsflash
New Coder

 
Join Date: Dec 2009
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
jdsflash is an unknown quantity at this point
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.
jdsflash is offline   Reply With Quote
Old 12-11-2009, 03:25 PM   PM User | #7
angst
Senior Coder

 
angst's Avatar
 
Join Date: Apr 2004
Location: Toronto, Ontario
Posts: 2,112
Thanks: 15
Thanked 122 Times in 122 Posts
angst is on a distinguished road
I just tested it and it works for me.
angst is offline   Reply With Quote
Old 12-11-2009, 03:28 PM   PM User | #8
jdsflash
New Coder

 
Join Date: Dec 2009
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
jdsflash is an unknown quantity at this point
http://www.flashmajic.com/test.html

you tested that page? what browser?
jdsflash is offline   Reply With Quote
Old 12-11-2009, 03:40 PM   PM User | #9
angst
Senior Coder

 
angst's Avatar
 
Join Date: Apr 2004
Location: Toronto, Ontario
Posts: 2,112
Thanks: 15
Thanked 122 Times in 122 Posts
angst is on a distinguished road
PHP Code:
<!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> 


PHP Code:

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.
angst is offline   Reply With Quote
Old 12-11-2009, 04:04 PM   PM User | #10
jdsflash
New Coder

 
Join Date: Dec 2009
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
jdsflash is an unknown quantity at this point
That works perfect! Thanks alot for you time and help.
jdsflash is offline   Reply With Quote
Reply

Bookmarks

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 01:21 AM.


Advertisement
Log in to turn off these ads.