...

View Full Version : Using a variable for a filename using <Script src|>



Sidders at CHEP
03-03-2009, 04:13 PM
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

Rowsdower!
03-03-2009, 06:51 PM
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):
<body onload="MyscriptUpdate()">
<div id="update_code_block" style="display:none;"></div>

And then this javascript:
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... :D

Let me know if this shows signs of life.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum