homepage Welcome to WebmasterWorld Guest from 54.211.95.201
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

This 36 message thread spans 2 pages: < < 36 ( 1 [2]     
javascript redirect with cookie
weddingm




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

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>

 

weddingm




msg:4639767
 3:39 am on Jan 24, 2014 (gmt 0)

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>

Fotiman




msg:4639770
 3:46 am on Jan 24, 2014 (gmt 0)

mobilename=get_cookie(mycookie);

mycookie is still undefined, so you need to have a line before that like:
mycookie = "somecookiename";

weddingm




msg:4639775
 4:22 am on Jan 24, 2014 (gmt 0)

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>

lucy24




msg:4639786
 5:52 am on Jan 24, 2014 (gmt 0)

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>

Fotiman




msg:4639920
 3:33 pm on Jan 24, 2014 (gmt 0)

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?

weddingm




msg:4640022
 3:12 am on Jan 25, 2014 (gmt 0)

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 ( 1 [2]
Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved