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 5 of 5
  1. #1
    rps
    rps is offline
    New Coder
    Join Date
    Sep 2010
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts

    vary call parameter

    When calling a javascript file I have written

    <script type = "javascript" src = "mycode_2010.js">

    However, I would like to replace 2010 with a variable so that for example I can detect the year and load the appropriate code e.g. mycode_2011.js rather than writing a new hard coded html page.

    Is it possible please?

  • #2
    Regular Coder
    Join Date
    Mar 2006
    Posts
    728
    Thanks
    35
    Thanked 132 Times in 123 Posts
    <script type = "javascript" src = "mycode_2010.js"></script>
    should be:
    <script type = "text/javascript" src = "mycode_2010.js"></script>

    or, in HTML5:
    <script src= "mycode_2010.js"></script>

    Code:
    <script>
    var y=new Date().getFullYear();
    document.writeln('<script  src="mycode_'+y+'.js"><\/script>');
    </script>
    By the way, this is a bad idea...

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,310
    Thanks
    203
    Thanked 2,563 Times in 2,541 Posts
    Well, of course it is possible!

    Code:
    <body onload = "loadjsfile()">
    
    <script type = "text/javascript">
    
    function loadjsfile(){
    var thisYear = new Date().getFullYear();
    var filename = "mycode_" + thisYear + ".js";
    var fileref=document.createElement('script');
    fileref.setAttribute("type","text/javascript");
    fileref.setAttribute("src", filename);
    if (typeof fileref != "undefined") {
    document.getElementsByTagName("head")[0].appendChild(fileref);
    }
    }
    
    </script>
    Longer than mrhoo's but could be executed other than at load time. For example, according to user's selection.

    Is it always a bad idea?

    Another way:-

    Code:
    <script type = "text/javascript" id="myscript"></script> 
    
    <script type = "text/javascript">
    var thisYear = new Date().getFullYear();
    var ms = document.getElementById("myscript");
    ms.src = "mycode_" + thisYear + ".js "; 
    </script>

    "He's an interesting player - he has unusually short back legs." - Football commentator
    Last edited by Philip M; 11-17-2010 at 06:04 PM.

  • #4
    rps
    rps is offline
    New Coder
    Join Date
    Sep 2010
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for your help. I can "play" with this. Why is it a bad idea incidentally? I'm simply trying to find a way of avoiding having to re-write code each year.

  • #5
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,075
    Thanks
    0
    Thanked 256 Times in 252 Posts
    Quote Originally Posted by Philip M View Post
    if (typeof fileref != "undefined") {
    document.getElementsByTagName("head")[0].appendChild(fileref);
    }
    I wonder why you have to check for undefined. fileref will never be undefined or null regardless if the js file exists or not.
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________


  •  

    Posting Permissions

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