Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 06-19-2011, 09:53 AM   PM User | #16
bullant
Banned

 
Join Date: Feb 2011
Posts: 2,699
Thanks: 13
Thanked 395 Times in 395 Posts
bullant is on a distinguished road
Quote:
Originally Posted by Philip M View Post
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.

Last edited by bullant; 06-19-2011 at 10:06 AM..
bullant is offline   Reply With Quote
Old 06-19-2011, 10:21 AM   PM User | #17
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,033
Thanks: 197
Thanked 2,410 Times in 2,388 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
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.
Philip M is offline   Reply With Quote
Old 06-19-2011, 10:26 AM   PM User | #18
bullant
Banned

 
Join Date: Feb 2011
Posts: 2,699
Thanks: 13
Thanked 395 Times in 395 Posts
bullant is on a distinguished road
Quote:
Originally Posted by Philip M View Post
My position is that these days Javascript is absolutely essential for web participation.
I agree if the functionality cannot be done server side

If it can be done server side then I will always do it server side first and then do it client side as well as an optional "extra" if it is required.

Like I said, personally I don't see any point in wasting time doing something client side if the same functionality can be done server side
bullant is offline   Reply With Quote
Old 06-19-2011, 01:08 PM   PM User | #19
kegra
New to the CF scene

 
Join Date: Jun 2011
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
kegra is an unknown quantity at this point
Quote:
Originally Posted by Philip M View Post
To set a cookie which will expire in one hour:-

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

john
kegra is offline   Reply With Quote
Old 06-19-2011, 01:27 PM   PM User | #20
bullant
Banned

 
Join Date: Feb 2011
Posts: 2,699
Thanks: 13
Thanked 395 Times in 395 Posts
bullant is on a distinguished road
Quote:
Originally Posted by kegra View Post
@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.
bullant is offline   Reply With Quote
Old 06-19-2011, 01:40 PM   PM User | #21
kegra
New to the CF scene

 
Join Date: Jun 2011
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
kegra is an unknown quantity at this point
Quote:
Originally Posted by bullant View Post
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....
kegra is offline   Reply With Quote
Old 06-19-2011, 01:43 PM   PM User | #22
bullant
Banned

 
Join Date: Feb 2011
Posts: 2,699
Thanks: 13
Thanked 395 Times in 395 Posts
bullant is on a distinguished road
Quote:
Originally Posted by kegra View Post
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
Since css3 is not yet officially released and support for it varies in browsers, I'm not touching it for commercial work yet.
bullant is offline   Reply With Quote
Old 10-10-2012, 12:56 AM   PM User | #23
jmatuska
New to the CF scene

 
Join Date: Oct 2012
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
jmatuska is an unknown quantity at this point
[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
jmatuska is offline   Reply With Quote
Old 10-10-2012, 08:14 AM   PM User | #24
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,033
Thanks: 197
Thanked 2,410 Times in 2,388 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
[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.

Last edited by Philip M; 10-10-2012 at 08:20 AM..
Philip M is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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

Forum Jump


All times are GMT +1. The time now is 12:00 PM.


Advertisement
Log in to turn off these ads.