Results 1 to 7 of 7

Thread: load a .js file from a directory depending on day of the week

  1. #1
    Join Date
    Jul 2007
    Posts
    20
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default load a .js file from a directory depending on day of the week

    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
    Code:
    <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

  2. #2
    Join Date
    Sep 2007
    Location
    The Netherlands
    Posts
    1,396
    Thanks
    32
    Thanked 153 Times in 147 Posts
    Blog Entries
    34

    Default

    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>

  3. #3
    Join Date
    Dec 2008
    Location
    Portsmouth, UK
    Posts
    1,815
    Thanks
    2
    Thanked 425 Times in 419 Posts

    Default

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

  4. #4
    Join Date
    Sep 2007
    Location
    The Netherlands
    Posts
    1,396
    Thanks
    32
    Thanked 153 Times in 147 Posts
    Blog Entries
    34

    Default

    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>

  5. #5
    Join Date
    Jul 2007
    Posts
    20
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default

    Thanks for your fast replies, but none of this scripts does load a script, for example on monday monday.js.

  6. #6
    Join Date
    Sep 2007
    Location
    The Netherlands
    Posts
    1,396
    Thanks
    32
    Thanked 153 Times in 147 Posts
    Blog Entries
    34

    Default

    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>

  7. #7
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    29,135
    Thanks
    44
    Thanked 3,231 Times in 3,192 Posts
    Blog Entries
    12

    Default

    This is tested and works:

    Code:
    <!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/s...kdayscript.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/

    Code:
    <!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/s...ayscriptjq.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.
    Last edited by jscheuer1; 04-30-2014 at 03:51 AM. Reason: add about limitations, later - add jQuery, later still - add demos
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

Similar Threads

  1. Bookflip - Can I make it load images from a directory?
    By retroboutiques in forum Dynamic Drive scripts help
    Replies: 0
    Last Post: 07-09-2009, 12:48 AM
  2. load javascript depending time of day
    By Marquis in forum Looking for such a script or service
    Replies: 13
    Last Post: 10-31-2008, 09:08 AM
  3. Replies: 7
    Last Post: 09-23-2008, 06:01 PM
  4. file and directory permissions
    By kosi in forum PHP
    Replies: 3
    Last Post: 05-30-2007, 09:59 AM
  5. Load new page on monday of each week
    By greenday45 in forum Looking for such a script or service
    Replies: 6
    Last Post: 04-11-2007, 05:16 AM

Bookmarks

Posting Permissions

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