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

    Calling script from a html page

    I have to call the script below from my html page. How would I do that? Do I need to modify the script at all? Put in a function that returns the random title to the html page?

    New to javascript any help woudl be apreciated.

    html page that calls the script
    Code:
    <html>
    <head>
    
    <script type='text/javascript' src='titles.js'></script>
    <script>
      document.title = rantitle[ranNumber];  
    </script>
      
    </head>

    titles.js
    Code:
    <script type="text/javascript" language="javascript">   
        <!--//  
        if (parseInt(navigator.appVersion) >= 4) {  
            var num = 3;  
            var rantitle = new Array(num+1);  
            rantitle[1] = "Random Title1";  
            rantitle[2] = "Random Title1";  
            rantitle[3] = "Random Title1";  
            ranNumber = parseInt(num * Math.random() + 1);  
            document.title = rantitle[ranNumber];  
        }  
        //-->   
    </script>

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,037
    Thanks
    203
    Thanked 2,539 Times in 2,517 Posts
    Your .js file must not contain any HTML tags so delete

    <script type="text/javascript" language="javascript">
    <!--//

    //-->
    </script>

    if (parseInt(navigator.appVersion) >= 4) { is quite redundant. IE3 and NS3 have long been one with Nineveh and Tyre.

    <script>
    document.title = rantitle[ranNumber];
    </script>
    is also redundant. The .js script returns the value of document.title already.


    <script language=javascript> is long deprecated and obsolete. Use <script type = "text/javascript"> instead.
    The <!-- and //--> comment (hiding) tags have not been necessary since IE3. If you see these in some published script it is a warning that you are looking at ancient and perhaps unreliable code.

    To summarize:-

    Code:
    <html>
    <head>
    <script type='text/javascript' src='titles.js'></script>
    </head>
    The .js file:-

    Code:
           var num = 3;  
            var rantitle = new Array(num+1);  
            rantitle[1] = "Random Title1";  
            rantitle[2] = "Random Title1";  
            rantitle[3] = "Random Title1";  
            ranNumber = parseInt (num * Math.random() + 1); 
            document.title = rantitle[ranNumber];
    Last edited by Philip M; 12-12-2009 at 04:41 PM.

  • #3
    Senior Coder
    Join Date
    Mar 2005
    Location
    Portsmouth UK
    Posts
    4,513
    Thanks
    3
    Thanked 504 Times in 491 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" xml:lang="en" lang="en">
    
    <head>
      <title></title>
    <script  type="text/javascript">
    /*<![CDATA[*/
    
    // this can go in a external js
    function Title(){
     var rantitle = [];
     rantitle[0] = "Random Title1";
     rantitle[1] = "Random Title2";
     rantitle[2] = "Random Title3";
     ranNumber = parseInt (rantitle.length*Math.random());
     document.title = rantitle[ranNumber];
    }
    
    Title();
    
    /*]]>*/
    </script></head>
    
    <body>
    <input type="button" name="" value="Test" onclick="Title();"/>
    </body>
    
    </html>
    Vic

    God Loves You and will never love you less.

    http://www.vicsjavascripts.org/Home.htm

    If my post has been useful please donate to http://www.operationsmile.org.uk/


  •  

    Posting Permissions

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