Sridhar_Rao
In the zone
I am trying to implement visitor redirection depending on the browser screen size using javascript.
Whenever a visitor accesses the desktop version using a mobile device, the javascript detects the screen size.
Visitor is prompted to select between desktop version and mobile version.
Visitor's choice is registered in a cookey that is set to expire 20 years later. This is to prevent the repeated prompts.
I know the javascript code is clumsy, but this is what I have done. Suggestions to improve is welcomed.
This is working fine and the cookie is set.
In the mobile version, links are provided that allow visitor to shift back to desktop version. I am implementing this using following javascript code:
The link:
The javascript:
What I am attempting to do is to recreate a new cookie with new name/value (version=desktop). I tried editing the cookie by changing the value of version, but did not succeed.
But, this is not working. Sometimes, this script doesn't fire or the cookie value doesn't change.
Please help me solve this problem. I am not a computer programmer.
Whenever a visitor accesses the desktop version using a mobile device, the javascript detects the screen size.
Visitor is prompted to select between desktop version and mobile version.
Visitor's choice is registered in a cookey that is set to expire 20 years later. This is to prevent the repeated prompts.
I know the javascript code is clumsy, but this is what I have done. Suggestions to improve is welcomed.
Code:
if (screen.width <= 699) {
var cook = document.cookie.split(';');
var cname = "version";
var cookd = "";
for(var i=0; i < cook.length; i++) {
if ((cook[i].indexOf(cname)) != -1){
match = cook[i].replace(" ", "");
cookd = match.substr(cname.length + 1);
if (cookd == "mobile"){
break;
}
}
}
switch (cookd){
case "mobile":
document.location = "*www.mysite.com/mobi/";
break;
case "desktop":
break;
default:
var today = new Date();
var the_date = new Date("December 31, 2023");
var the_cookie_date = the_date.toGMTString();
var ans = confirm("You seem to be using a mobile device. A scaled down mobile version is available. Click OK to visit the mobile site.");
if (ans){
var the_cookie = "version=mobile";
var the_cookie = the_cookie + ";expires=" + the_cookie_date + "; path/";
document.cookie = the_cookie;
document.location = "*www.mysite.com/mobi/";
}else{
var the_cookie = "version=desktop";
var the_cookie = the_cookie + ";expires=" + the_cookie_date + "; path/";
document.cookie = the_cookie;
}
}
}
This is working fine and the cookie is set.
In the mobile version, links are provided that allow visitor to shift back to desktop version. I am implementing this using following javascript code:
The link:
Code:
<a href="javascript:goversion('mypage.htm')">Desktop version</a>
The javascript:
Code:
function goversion(site){
loca = "*www.mysite.com/"+site
if (screen.width <= 699) {
var today = new Date();
var the_date = new Date("December 31, 2023");
var the_cookie_date = the_date.toGMTString();
var the_cookie = "version=desktop";
var the_cookie = the_cookie + ";expires=" + the_cookie_date + "; path/";
document.cookie = the_cookie;
}
document.location = loca;
}
What I am attempting to do is to recreate a new cookie with new name/value (version=desktop). I tried editing the cookie by changing the value of version, but did not succeed.
But, this is not working. Sometimes, this script doesn't fire or the cookie value doesn't change.
Please help me solve this problem. I am not a computer programmer.