Computer Forums

Member Login

Remember Me? Sign Up! | Forgot Password
 
Slogan
 
Closed Thread
Old 07-13-2004, 10:41 AM   #1 (permalink)
 
Newb Techie

Join Date: May 2004

Posts: 34

confusedideal

Default external javascript

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?
confusedideal is offline  
Old 07-13-2004, 02:30 PM   #2 (permalink)
 
Newb Techie

Join Date: May 2004

Posts: 34

confusedideal

Default

Or at least could someone point me in the direction of some free counter script or a free counter?
confusedideal is offline  
Old 07-13-2004, 06:27 PM   #3 (permalink)
 
Monster Techie

Join Date: Jul 2003

Posts: 1,179

Emily is on a distinguished road

Send a message via AIM to Emily
Default

http://www.google.com/search?hl=en&i...=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.
__________________
<a href=\"http://www.upstark.com\">www.upstark.com</a>
Emily is offline  
Old 07-15-2004, 10:06 AM   #4 (permalink)
milen's Avatar
 
Super Techie

Join Date: Feb 2004

Posts: 304

milen is on a distinguished road

Default

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()">

????????????????????????
milen is offline  
Old 07-15-2004, 10:24 AM   #5 (permalink)
milen's Avatar
 
Super Techie

Join Date: Feb 2004

Posts: 304

milen is on a distinguished road

Default

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
..
...
....
milen is offline  
Old 07-15-2004, 10:33 AM   #6 (permalink)
milen's Avatar
 
Super Techie

Join Date: Feb 2004

Posts: 304

milen is on a distinguished road

Default

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
milen is offline  
 
Closed Thread

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On