Only a tiny minority of people disable Javascript in their browser
that may or may not be true, but in either case if something can be done both client and server side, I will always do it server side first so I don't have to have a plan B for javascript disabled browsers and then do it client side as an optional "extra" if required.
If you want to determine if something has already been done, prior to loading the page in the browser, on a given day you don't need javascript or cookies. You can do it server side.
I don't see any point in wasting time with javascript if I can do exactly the same thing server side.
My position is that these days Javascript is absolutely essential for web participation. Most sites rely on Javascript for functionality, not just decoration. Everyone knows that (all) Javascripts will not work if Javascript is disabled in the browser. They will not work if the computer is not connected to a power source either. The point you make is valid, but utterly trivial.
Many banking (and other) sites point out that you must have Javascript and cookies enabled if you are to be able to use them.
__________________
All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
<script type = "text/javascript">
function createCookie(name, value){
var today = new Date;
today.setTime(today.getTime()+ 3600000); // = 1 hour
document.cookie = name + "=" + value + "; expires=" + today.toGMTString();
alert (name + " value is: "+ value + "\nExpires in: " + today);
}
createCookie("myName", "myValue");
</script>
Then start by reading the cookie - if it is found do this, if it is not found do that.
Code:
<script type="text/javascript">
function createCookie(name,value,days) {
if (days) {
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
}
else var expires = "";
document.cookie = name+"="+value+expires+"; path=/";
}
Thank you for your quick answer , as i have stated iam inexperienced and found that by just altering your previous code replacing 24 with 1 in
Code:
date.setTime(date.getTime()+(days*1*60*60*1000));
does the job for me by creating a cookie with 1 hour validity , it was much more effort for me to use the new script you provided than alter the previous one. in any case thank you and appreciate your help.
@bullant yes i think server side is always better cause you have total control but already as philip said my site relies heavily on jscript so its not an option for me. thank you for your suggestion.
@bullant yes i think server side is always better cause you have total control but already as philip said my site relies heavily on jscript so its not an option for me. thank you for your suggestion.
No problem . Was just making sure you were aware of other possible options.
No problem . Was just making sure you were aware of other possible options.
also to make fun of the situation (how much i rely on jscript) i am using ccs3 transformations that work flawlesly on webkit browsers except IE
eg i use rotate and skew on html elements but for this to work i have a jscript (the excellent work of Zoltan sand transformations) that transforms the transformations LOL into IE understandable format....
[QUOTE=Philip M;993154]You need to set a cookie. Many examples have been posted in this forum - here is one of them. You set the expiry date to the desired number of days ahead (here 365 days - change yours to 1 or whatever).
Code:
<html>
<head>
<title>Welcome Message Displays Once Only</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">
function createCookie(name,value,days) {
if (days) {
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
}
else var expires = "";
document.cookie = name+"="+value+expires+"; path=/";
}
function readCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
}
function eraseCookie(name) {
createCookie(name,"",-1);
}
</script>
</head>
<body>
<script type="text/javascript">
if(!readCookie('visitedPreviously')){
document.write(' Your Message Goes Here And You See It Only Once ');
createCookie('visitedPreviously', 'visitedPreviously', 365); // 365 days persistence
}
//eraseCookie('visitedPreviously'); // FOR TEST PURPOSES
</script>
</body>
</html>
Note that "a day" means 24 hours, not exactly the same thing as the next day starting at midnight.
Thanks Phillip M. This is great! (#2 response)
However, is it possible to make the message appear in an alert box rather than just on the page itself? I'm guessing that 'document.write' puts it on the page. Is there something that will create an alert box instead?
jmatuska
[QUOTE=jmatuska;1278167Thanks Phillip M. This is great! (#2 response)
However, is it possible to make the message appear in an alert box rather than just on the page itself? I'm guessing that 'document.write' puts it on the page. Is there something that will create an alert box instead?
jmatuska[/QUOTE]
Well, alert boxes are not considered a very professional way of displaying information to a user.
But if you replace document.write by alert then you ought to get an alert.
But naturally you could display the message in a <span> styled in whatever fancy way you wish.
__________________
All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.