Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    New Coder
    Join Date
    Dec 2009
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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>

  • #2
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    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>

  • #3
    Regular Coder
    Join Date
    Jan 2006
    Posts
    568
    Thanks
    6
    Thanked 84 Times in 84 Posts
    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

  • #4
    New Coder
    Join Date
    Dec 2009
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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>

  • #5
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    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>

  • #6
    New Coder
    Join Date
    Dec 2009
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #7
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    I just tested it and it works for me.

  • #8
    New Coder
    Join Date
    Dec 2009
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts
    http://www.flashmajic.com/test.html

    you tested that page? what browser?

  • #9
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    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.

  • #10
    New Coder
    Join Date
    Dec 2009
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts
    That works perfect! Thanks alot for you time and help.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •