Log in

View Full Version : load a .js file from a directory depending on day of the week



Marquis
04-27-2014, 02:36 PM
Need to load a .js file from a directory depending on day of the week. If its monday load Monday.js. I can not figure this out .. i tried this

<script type="text/javascript">

var d=new Date();
var weekday=new Array(7);
weekday[0]="Sunday";
weekday[1]="Monday";
weekday[2]="Tuesday";
weekday[3]="Wednesday";
weekday[4]="Thursday";
weekday[5]="Friday";
weekday[6]="Saturday";
var n = weekday[d.getDay()];
var jsFile = n + ".js";
$("#myDiv").load(jsFile);

</script>

in body <div id="myDiv"></div>

but don't work

thanks in advance

molendijk
04-27-2014, 03:28 PM
You don't need jquery for that. Just put:

<body>
<div id="myDiv"></div>

<script type="text/javascript">
// script immediately before closing body tag
var d=new Date();
var weekday=new Array(7);
weekday[0]="Sunday";
weekday[1]="Monday";
weekday[2]="Tuesday";
weekday[3]="Wednesday";
weekday[4]="Thursday";
weekday[5]="Friday";
weekday[6]="Saturday";
var n = weekday[d.getDay()];
document.getElementById('myDiv').innerHTML=n
</script>
</body>

vwphillips
04-27-2014, 03:30 PM
<!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>
</head>

<body>

<script type="text/javascript">

var d=new Date();
var weekday=["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];
var n = weekday[d.getDay()];
var jsFile = n + ".js";
var js=document.createElement('SCRIPT');
js.type='text/javascript';
js.src=jsFile;
document.getElementsByTagName('HEAD')[0].appendChild(js);
// $("#myDiv").load(jsFile);

</script>
</body>

</html>

molendijk
04-27-2014, 03:47 PM
Vic, your code doesn't work here.
If we want to use append, I would prefer the jquery-way.

<!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>
</head>

<body>

<script src="http://code.jquery.com/jquery-1.10.2.js"></script>

<script type="text/javascript">
var d=new Date();
var weekday=["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];
var n = weekday[d.getDay()];
$("body").append(n);
</script>
</body>

</html>

Marquis
04-27-2014, 10:10 PM
Thanks for your fast replies, but none of this scripts does load a script, for example on monday monday.js.

molendijk
04-27-2014, 10:42 PM
Sorry for the misunderstanding, I thought you only wanted the script to show the days of the week.
And also apologies to Vic; his code does exactly want you want IF THE SCRIPTS FOR THE DAYS OF THE WEEK ARE IN THE SAME FOLDER AS THE FILE WHERE YOU INVOKE THEM.
If you want to put them in a separate folder, say 'days_of_week', then Vics script would look like:

<!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>
</head>

<body>

<script type="text/javascript">

var d=new Date();
var weekday=["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];
var n = weekday[d.getDay()];
var jsFile = n + ".js";
var js=document.createElement('SCRIPT');
js.type='text/javascript';
js.src='days_of_week/'+jsFile;
document.getElementsByTagName('HEAD')[0].appendChild(js);

</script>
</body>

</html>

jscheuer1
04-28-2014, 05:11 PM
This is tested and works:


<!DOCTYPE html>
<html>
<head>
<title>Load a Day of the Week Script</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link rel="shortcut icon" href="http://home.comcast.net/~jscheuer1/favicon.ico" />
</head>
<body>
<script type="text/javascript">
(function(){
var s = document.createElement('script');
s.type = 'text/javascript';
s.src = ['sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday'][new Date().getDay()] + '.js?bustcache=' + new Date().getTime();
document.getElementsByTagName('head')[0].appendChild(s);
})();
</script>
</body>
</html>

Demo: http://home.comcast.net/~jscheuer1/side/demos/tidbits/dow/weekdayscript.htm

The browser cache may need to be cleared and/or the page refreshed to see changes.

There may be some limitations upon what such a script (monday.js, tuesday.js, etc.) can do, for example, if they include the document write or writeLn methods, they might overwrite the existing page. But most things will work as expected. And in the above code, these day scripts must be in the same folder as the page that's using them (although they could, with a minor change to the code go in another folder).

If you want to use jQuery, use:

http://api.jquery.com/jQuery.getScript/


<!DOCTYPE html>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

</head>
<body>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8/jquery.min.js"></script>
<script type="text/javascript">
$.getScript(['sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday'][new Date().getDay()] + '.js');
</script>
</body>
</html>
Demo: http://home.comcast.net/~jscheuer1/side/demos/tidbits/dow/weekdayscriptjq.htm

If you want more help, please include a link to the page on your site that contains the problematic code so we can check it out.