external javascript

Status
Not open for further replies.

confusedideal

Baseband Member
Messages
34
I'm trying to build my own javascript counter. I'm using an external javascript file with the following code in it:

var Counter = Counter + 1
document.write("This page has been visited " + Counter + " times.")

Pretty basic stuff. However, when I run this in my browser, I get this:

This page has been visited NaN times.

Is there a way to initialize the Counter variable once and not worry about it anymore?
 
http://www.google.com/search?hl=en&ie=UTF-8&q="free+counter"&btnG=Google+Search

The problem with your code is that every time the page is loaded (when the counter should be increased by 1) the variable is re-dimensioned, meaning that the original variable is lost and a new one (initialized to nothing) is created. To make a counter you need to store the value in a file or database - someplace that isn't overwritten every time the page loads.

P.S. The NaN is an error because you're attempting to add 1 to a value (Counter) that doesn't exist yet (because the value disappeared when the page reloaded). With the code below, you get rid of the NaN error, but obviously it doesn't function as a counter until you implement a file or database that holds the value permanently.

var Counter = 0;
Counter = Counter + 1;
document.write("This page has been visited " + Counter + " times.");

This works because you have defined the Counter variable and set it to a value (0); then, once it's been set to a value, you can add 1 to it.
 
Is this the whole code for that operation or just part of it?

<html>
<title> Hahe Script</title>
<head>
<style>
body{font-size:very-tight}
</style>
</head>
<body>
<script language="JavaScript">
var Counter = 0;
Counter = Counter + 1;
document.write("This page has been visited " + Counter + " times.");
}
</script>

some more text
<body>
</html>

--
Or must be with "function Hahe()" and then <body onload="Hahe()">

????????????????????????
 
You can try this script:
.
..
...
</head>
<script language="javaScript" type="text/Javascript">
function hahe2(thetruth){
alert(thetruth)
}
</script>
<body bgcolor="red">
<form>
<input type="button" value="Why not click ME?" onClick="hahe2('Do some body believe in counters?hah?')">
</form>
Some more text
..
...
....
 
Do you think you will keep somebody on your site if the counter point 3 or 4?

This is code for fake counter:

<script language="JavaScript">
var iHaveManyUsers
iHaveManyUsers = Math.round(Math.random()*50000)
function alertUser() {
alert("You are visitor number " + iHaveManyUsers + " to this page.")
}
</script>
<body>
<form>
<input type="button" VALUE="How many visits?" onClick="alertUser()">
</form>

___
you can change the alert() with document.write... del the form
-----

If you had enough of codes:
Edit the page every week.
<body>
<center>You are visitor: 2345</center>
----
Next week
<body
<center> You are visitor: 3678</center>
this will dooo
 
Status
Not open for further replies.
Back
Top Bottom