View previous topic :: View next topic |
Author |
Message |
RTAdams89 -
Joined: 06 Nov 2005 Posts: 102
|
Posted: Tue Jan 30, 2007 11:59 pm Post subject: Alternative to cookies |
|
|
I have a website that is designed for a particular monitor resolution. When a person with a smaller screen setting visits the page, a popup window notifies them that it appears better at a larger resolution. I didn't want to annoy my visitors too much, so the site also sets a cookie that prevents the message from being displayed on subsequent visits. The code is as follows:
Code: | <SCRIPT type="text/javascript">
function SetCookie(name, value, days) {
var expire = new Date ();
expire.setTime (expire.getTime() + (24 * 60 * 60 * 1000) * days);
document.cookie = name + "=" + escape(value) + "; expires=" +expire.toGMTString();
}
function GetCookie(name) {
var startIndex = document.cookie.indexOf(name);
if (startIndex != -1) {
var endIndex = document.cookie.indexOf(";", startIndex);
if (endIndex == -1) endIndex = document.cookie.length;
return unescape(document.cookie.substring(startIndex+name.length+1, endIndex));
}
else {
return null;
}
}
var correctwidth=1024
var correctheight=768
if (screen.width!=correctwidth||screen.height!=correctheight)
{
if(GetCookie('screensizealerted')==null)
{
SetCookie('screensizealerted', 'screensizealerted', 365);
alert("This webpage is bested viewed with screen resolution "+correctwidth+"*"+correctheight+". Your current resolution is "+screen.width+"*"+screen.height+". If you experience problems while viewing this site, please try changing the resolution to "+correctwidth+"*"+correctheight+". You will not receive this warning again.");
}
}
</SCRIPT> |
However, this doesn't work if the cookie is blocked. Is there a better way to go about this? Perhaps by recording the IP address on the server end? |
|
Back to top |
|
|
pkSML -
Joined: 29 May 2006 Posts: 952 Location: Michigan, USA
|
Posted: Thu Feb 01, 2007 10:02 pm Post subject: |
|
|
Just a thought...
Have one JavaScript set the cookie that loads at the beginning of the HTML.
At the end of the HTML, have a Javascript routine that checks to see if the cookie was written. If not, give them another message, which can be on the page somewhere.
Example from http://stephen.calvarybucyrus.org/comics: Code: | <SCRIPT>
function write(){
document.all.loading.innerHTML = "Cookies are not on. Please enable them for a more personalized browsing experience.";
}
</SCRIPT> |
It will replace the text inside the <div> tag looking like this: Code: | <div id="loading" style="position: absolute; top: 46px; right: 80px; background: white;">Cookies are on.</div> |
You just have to call the function, like this: Code: | <script>write()</script> |
You might also see http://stephen.calvarybucyrus.org/search/javascript+if+cookie+fails _________________ Stephen
Need a LitlURL?
http://CodeBin.yi.org |
|
Back to top |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|