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 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Mar 2009
    Location
    Farnborough, Hampshire UK
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Using a variable for a filename using <Script src|>

    Hello all. I am almost completely new to web design and use FrontPage to create pages. I am, therefore, also almost completely new to working with javascript and tend to jazz up web pages by taking advantage of code that people make available on web sites.

    I am currently trying to use the Script src tag as follows:

    <script language=JavaScript1.2 src='MyScript.js'></script>

    However, I am trying to write it so that I have a variable for the src parameter. This is so that when I change the name of the script file, the src will still point to the correct "new" file. The resaon I need this is that using a new file name would "force" my user's browsers to pick up the new file without having to clear the cache.

    Further up in the code I have written this:

    <script language=JavaScript>
    var today = new Date()
    var myyear = today.getFullYear()*10000
    var mymonth = (today.getMonth()+1)*100
    var myday = today.getDay()+1
    var Mydate = myyear+mymonth+myday
    var MyLoader = MyScript" + Mydate + ".js"

    which, I beleive, creates a variable called MyLoader with the value:

    MyScriptyyyymmdd.js

    where yyyymmdd is today's date.

    Then simply by renaming my script file to MyScript20090303.js for example, this is the file that should be accessed.

    However, when I replace by calling line with

    <script language=JavaScript1.2 src=MyLoader></script>

    I get an error and the script file does not get run.

    Is there a way round this or have I missed something out to tell the script command that MyLoader is a variable?

    Many thanks for any help I receive!


    Sidders

  • #2
    Senior Coder Rowsdower!'s Avatar
    Join Date
    Oct 2008
    Location
    Some say it's everything.
    Posts
    2,027
    Thanks
    5
    Thanked 397 Times in 390 Posts
    Hello and welcome to the forums!

    Why not try something like this in the HTML (alter your existing body tag and place a dummy div):
    Code:
    <body onload="MyscriptUpdate()">
    <div id="update_code_block" style="display:none;"></div>
    And then this javascript:
    Code:
    function MyscriptUpdate() {
    var today = new Date()
    var myyear = today.getFullYear()*10000
    var mymonth = (today.getMonth()+1)*100
    var myday = today.getDay()+1
    var Mydate = myyear+mymonth+myday
    var MyLoader = MyScript" + Mydate + ".js"
    document.getElementById('update_code_block').innerHTML="<script type='text/javascript' src='"+MyLoader+"'></script>"
    }
    I'm kind of crappy at Javascript so there's probably a better way than using that dummy div to load the other script file, but I don't know how...

    Let me know if this shows signs of life.
    The object of opening the mind, as of opening the mouth, is to shut it again on something solid. G.K. Chesterton
    See Mediocrity in its Infancy
    It's usually a good idea to start out with this at the VERY TOP of your CSS: * {border:0;margin:0;padding:0;}
    Seek and you shall find... basically:
    validate your markup | view your page cross-browser/cross-platform | free web tutorials | free hosting


  •  

    Posting Permissions

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