Results 1 to 6 of 6

Thread: how to display .js script

  1. #1
    Join Date
    Apr 2006
    Location
    London
    Posts
    77
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default how to display .js script

    Hello all, I'm using the following script to display the date.

    But to decrease my page loading time, I'll be putting the script inside a .js file.

    If I do that, how can I get the date to be displayed at a specific area on the webpage, as the document.write is in the script itself..

    the script:
    Code:
    <script language="JavaScript">
      <!--
        var now = new Date();
        var days = new Array(
          'Sunday','Monday','Tuesday',
          'Wednesday','Thursday','Friday','Saturday');
        var months = new Array(
          'January','February','March','April','May',
          'June','July','August','September','October',
          'November','December');
        var date = ((now.getDate()<10) ? "0" : "")+ now.getDate();
        function fourdigits(number)	{
          return (number < 1000) ? number + 1900 : number;}
        today =  days[now.getDay()] + ", " +
           months[now.getMonth()] + " " +
           date + ", " +
           (fourdigits(now.getYear()));
         document.write(today);
      //-->
    </script>
    thanks,
    VatsaL

  2. #2
    Join Date
    May 2006
    Location
    Sydney, Australia - Near the coast.
    Posts
    1,995
    Thanks
    0
    Thanked 8 Times in 7 Posts

    Default

    Put the <script..........> tag where you want it, or put document.write(.....) where you want it.
    Peter - alotofstuffhere[dot]com - Email Me - Donate via PayPal - Got spare hardware? Donate 'em to me :) Just send me a PM.
    Currently: enjoying the early holidays :)
    Read before posting: FAQ | What you CAN'T do with JavaScript | Form Rules | Thread Title Naming Guide

  3. #3
    Join Date
    Dec 2004
    Location
    UK
    Posts
    2,358
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by VatsaL View Post
    But to decrease my page loading time, I'll be putting the script inside a .js file.

    If I do that, how can I get the date to be displayed at a specific area on the webpage, as the document.write is in the script itself..
    You have two choices. Either write a function, include that somewhere, then include a script element that's only purpose is to call that function, or include the file at the point where the write method should be executed.

    <script language="JavaScript">
    The language attribute has been deprecated for a very long time. Use the type attribute, instead:

    HTML Code:
    <script type="text/javascript">
    <!--
    The reasons for "hiding" scripts haven't applied in years. All browsers now in use recognise script elements, even if they can't execute them.

    Code:
        function fourdigits(number)	{
          return (number < 1000) ? number + 1900 : number;}
        today =  days[now.getDay()] + ", " +
           months[now.getMonth()] + " " +
           date + ", " +
           (fourdigits(now.getYear()));
    The odd format aside, use the getFullYear method, rather than getYear.

    Mike

  4. #4
    Join Date
    Apr 2006
    Location
    London
    Posts
    77
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thanks. And one more thing.

    About the Dynamic Drive script for the date

    http://www.dynamicdrive.com/dynamicindex6/clock3.htm

    It uses <body onload>, Is it more simpler than the original script that I posted?
    I'm using the script for building a custom homepage.

    Which one is better, in terms of fast page loading, browser compatibility and simpicity?

  5. #5
    Join Date
    Dec 2004
    Location
    UK
    Posts
    2,358
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by VatsaL View Post
    http://www.dynamicdrive.com/dynamicindex6/clock3.htm

    It uses <body onload>, Is it more simpler than the original script that I posted?
    No, they do different things, and as a consequence, the code you posted is much simpler.

    Which one is better, in terms of fast page loading, browser compatibility and simpicity?
    There's not much in it between the two, though the script you linked to is rather dated.


    A polished version of the code you posted:

    Code:
    (function() {
        var now = new Date(),
            date = '0' + now.getDate(),
            days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'],
            months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August',
                'September', 'October', 'November', 'December'];
    
        document.write(days[now.getDay()] + ', ' + months[now.getMonth()] + ' '
            + date.substring(date.length - 2) + ' ' + now.getFullYear());
    })();
    Again, you'd just include the script element at the point you'd like the date to appear.

    Mike

  6. #6
    Join Date
    Apr 2006
    Location
    London
    Posts
    77
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Works spot on. Cheers.

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
  •