...

View Full Version : Resolved How do open external .js files?



sirbassface
12-18-2011, 05:44 PM
Hi everyone,

I'm a newbie and this is probably really easy but it just won't work!
I've tried looking at various online sources but they are all either too technical or don't seem to work. I'm obviously doing something wrong!

The code is posted below, it's not very long so hopefully someone will be able to debug it!

Thanks lots!

Original page:



<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>variables</title>
</head>
<body>
<script src="displaydate.js" type="text/javascript"></script>
<br>
</body>
</html>



External displaydate.js file:



function todaydate(){
var today_date= new Date()
var myyear=today_date.getYear()
var mymonth=today_date.getMonth()+1
var mytoday=today_date.getDate()
document.write(myyear+"/"+mymonth+"/"+mytoday)
}


Opening the original page in my browser displays nothing. Just a blank page.

Any help you can give would be greatly appreciated!

Geoff

theTortoise
12-18-2011, 06:23 PM
Thats because you have not called your function, try this:



<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>variables</title>
<script src="displaydate.js" type="text/javascript"></script>
</head>
<body onload="todaydate()">

<br>
</body>
</html>

felgall
12-18-2011, 07:14 PM
Thats because you have not called your function, try this:



<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>variables</title>
<script src="displaydate.js" type="text/javascript"></script>
</head>
<body onload="todaydate()">

<br>
</body>
</html>


That isn't going to work because the document.write must be run BEFORE the page finishes loading or it will overwrite te entire page.

The simplest way is to just call the function immediately after defining it.

sirbassface
12-18-2011, 08:07 PM
That isn't going to work because the document.write must be run BEFORE the page finishes loading or it will overwrite te entire page.

The simplest way is to just call the function immediately after defining it.

Thankyou both for your help.
Felgall could you please give me an example of what you mean? I have tried this





<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>variables</title>
</head>
<body>
<br>
Is this working now? Who knows?
<script src="displaydate.js" type="text/javascript">
document.write(myyear+"/"+mymonth+"/"+mytoday)
</script>
</body>
</html>



and I've tried adding the



<script src="displaydate.js" type="text/javascript">
document.write(myyear+"/"+mymonth+"/"+mytoday)


to the head section. Neither seem to work.
Where exactly should I call the function immediately after defining it?

Thanks again for the advice!

cuzMazn
12-18-2011, 10:33 PM
Where exactly should I call the function immediately after defining it?


Hi there,

Please replace your:



function todaydate() {
var today_date = new Date();
var myyear = today_date.getYear();
var mymonth = today_date.getMonth() + 1;
var mytoday = today_date.getDate();
document.write(myyear + "/" + mymonth + "/" + mytoday);
};


By this:



(function todaydate() {
var today_date = new Date();
var myyear = today_date.getYear();
var mymonth = today_date.getMonth() + 1;
var mytoday = today_date.getDate();
document.write(myyear + "/" + mymonth + "/" + mytoday);
})();


This will make your code run immediately after it is loaded.

Explanation:

If you have a code:



function bakeThatUnicorn() {
alert("BAKED!");
};


And you call it:



bakeThatUnicorn();


You just simply parse the variable bakeThatUnicorn (which returns the function bakeThatUnicorn), then executed the parsed result by adding ().

The same theory applied, instead of call it through a variable like that function name, you can immediately parse the function, and execute it.



(function bakeThatUnicorn() {
//Fancy stuffs with fancy recipe
})();


Those parenthesis () are used to parse anything (for example, (1 + 2) will parse 1 + 2 and return 3). So in this case, it will parse the function. Then just simply execute it by adding ().

I hope this help.

Tim_



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum