Welcome to WebmasterWorld Guest from 54.167.213.22

Forum Moderators: open

Message Too Old, No Replies

javascript redirect with cookie

     
6:21 am on Jan 8, 2014 (gmt 0)

5+ Year Member



I have the following code which works great in the html head:

<script type="text/javascript">
<!--
if (screen.width <= 699) {
window.location = ("http://sample.com");
}
</script>


However, I cannot get this to work? Any help?

<script type="text/javascript">
<!--
if (screen.width <= 699) {
var mobilec = GetCookie('mobileredirect');
if (mobilec!='mobi') {
window.location = ("http://m.sample.com");}
}
</script>
3:39 am on Jan 24, 2014 (gmt 0)

5+ Year Member



I think I am closer. Any Advise as still not working.

<script language="JavaScript" type="text/javascript"> 
function get_cookie ( cookie_name )
{
var cookie_string = document.cookie ;
if (cookie_string.length != 0) {
var cookie_value = cookie_string.match (
'(^|;)[\s]*' +
cookie_name +
'=([^;]*)' );
return decodeURIComponent ( cookie_value[2] ) ;
}
return '' ;
}

mobilename=get_cookie(mycookie);
if(mobilename.length == 0) {mobilename='newvisitor'; }

else
{
if ((mobilename!='myvalue') && (screen.width <= 699))
{window.location = "http://m.url.com"; } }

</script>
3:46 am on Jan 24, 2014 (gmt 0)

WebmasterWorld Senior Member fotiman is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



mobilename=get_cookie(mycookie);

mycookie is still undefined, so you need to have a line before that like:
mycookie = "somecookiename";
4:22 am on Jan 24, 2014 (gmt 0)

5+ Year Member



Thanks I got the redirect to work. Now the cookie will not be set. I am guessing so as it will not stop redirecting.

desktop site code:

<script language="JavaScript" type="text/javascript"> 
function getCookie(c_name) {
if (document.cookie.length>0)
{
c_start=document.cookie.indexOf(c_name + "=")
if (c_start!=-1)
{
c_start=c_start + c_name.length+1
c_end=document.cookie.indexOf(";",c_start)
if (c_end==-1) c_end=document.cookie.length
return unescape(document.cookie.substring(c_start,c_end))
}
}
return ""
}

mycookie = "mycookie";
mobilename = "newvisitor";
mobilename=getCookie(mycookie);

if ((mobilename!='myvalue') && (screen.width <= 699))
{window.location = "http://m.url.com"; }

</script>


Above works. However, below is the mobile site to set the cookie. The mobile site is on m.url.com.

<script language="JavaScript" type="text/javascript"> 
mycookie = "mycookie";
myvalue = "myvalue";
function set_mobile_cookie() {
if (!isset($_COOKIE['mycookie']))
{ setcookie("mycookie", myvalue, time()+3600, "/", ".url.com");
}}
add_action( 'init', 'set_mobile_cookie');
</script>
5:52 am on Jan 24, 2014 (gmt 0)

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



add_action( 'init', 'set_mobile_cookie');

Does this formulation work for you in other contexts? Any difference if the function is called by some other means, such as "onload" or in response to a specified user action?

This isn't a WordPress site is it?

<tangent>
setcookie("mycookie", myvalue, time()+3600, "/", ".url.com")

Time in seconds, right? So if users go away for longer than an hour, the site forgets that they didn't want to be redirected? Even if it were time in minutes, you're only giving them two and a half days. Either make it a session cookie, or give it a decent time period like three months. If users change their minds and want to be redirected, they'll say so.
</tangent>
3:33 pm on Jan 24, 2014 (gmt 0)

WebmasterWorld Senior Member fotiman is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



Ok, first lets look at your "desktop" site code. Don't include a language attribute on script tags, it's not used and not valid. Be sure to declare your variables using var. And don't forget your semi colons at the end of statements. You've also assigned a string value "newvisitor" to mobilename, but then you immediately replace it with the call to getCookie, so lets drop that first assignment. Here's a cleaned up version:

<script type="text/javascript">
function getCookie(c_name) {
var c_start, c_end;
if (document.cookie.length > 0) {
c_start = document.cookie.indexOf(c_name + "=");
if (c_start != -1) {
c_start = c_start + c_name.length + 1;
c_end = document.cookie.indexOf(";", c_start)
if (c_end == -1) {
c_end = document.cookie.length;
}
return unescape(document.cookie.substring(c_start, c_end));
}
}
return "";
}

var mycookie = "mycookie";
var mobilename = getCookie(mycookie);

if ((mobilename != 'myvalue') && (screen.width <= 699)) {
window.location = "http://m.url.com";
}
</script>


Next, lets think about the logic of this. You're code is doing this:
1. Get a cookie named "mycookie"
2. If the value of that cookie is not "myvalue", AND the screen width is less than or equal to 699, then redirect.

So you will always redirect the first time if the screen width is <= 699.

Now lets look at the m.url.com code.


<script language="JavaScript" type="text/javascript">
mycookie = "mycookie";
myvalue = "myvalue";

function set_mobile_cookie() {
if (!isset($_COOKIE['mycookie'])) {
setcookie("mycookie", myvalue, time() + 3600, "/", ".url.com");
}
}
add_action('init', 'set_mobile_cookie');
</script>

That looks like PHP code, not JavaScript. There is no "isset" method in JavaScript, but there is in PHP. Also, $_COOKIE is a PHP variable. So I think you either want to move that code to your PHP code, or you want to replace that with a JavaScript version.

Here's what I think you're trying to do:
1. If the cookie named "mycookie" does not exist, set it to "myvalue", set it to expire in 3600 seconds (1 hour), and apply it to the ".url.com" domain (including all subdomains like m.url.com and www.url.com).

With that, if the user visits the desktop site again, they will not get redirected to the mobile site until 1 hour has elapsed. Is that what you're trying to achieve?
3:12 am on Jan 25, 2014 (gmt 0)

5+ Year Member



Thank you Fotiman. That is exactly what I am trying to do. Yes, lucy24 and Fotiman, I am using as PHP, sorry for the confusion - in Wordpress. I will repost the code in the php forum tomorrow unless someone here can help tonight.

Thanks all!
This 36 message thread spans 2 pages: 36
 

Featured Threads

Hot Threads This Week

Hot Threads This Month