PDA

View Full Version : how to display .js script



VatsaL
11-17-2006, 01:49 AM
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:

<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

tech_support
11-17-2006, 02:35 AM
Put the <script..........> tag where you want it, or put document.write(.....) where you want it.

mwinter
11-17-2006, 02:36 AM
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:



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





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

VatsaL
11-17-2006, 12:39 PM
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?

mwinter
11-17-2006, 11:53 PM
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:



(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

VatsaL
11-18-2006, 02:57 AM
Works spot on. Cheers.