Welcome to WebmasterWorld Guest from 50.19.57.50

Forum Moderators: open

Message Too Old, No Replies

javascript redirect with cookie

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

Junior Member

10+ Year Member

joined:Mar 9, 2006
posts: 177
votes: 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>
2:32 pm on Jan 8, 2014 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:May 23, 2005
posts:742
votes: 0


Java is not the same thing as JavaScript.

You are missing a curly brace after the first var mobilec = ....
2:35 pm on Jan 8, 2014 (gmt 0)

Senior Member from US 

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

joined:Oct 17, 2005
posts: 4988
votes: 12


1. The title of this post is incorrect. Java != JavaScript.
2. Your example includes an HTML opening comment (<!--) but no matching closing (-->). HTML comments should be left out of script tags. They were needed for Netscape 1 and haven't been needed for about 15 years.
3. Where is the GetCookie function defined?

[edited by: phranque at 8:43 pm (utc) on Jan 21, 2014]
[edit reason] disabled graphic smileys [/edit]

2:38 pm on Jan 8, 2014 (gmt 0)

Senior Member from US 

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

joined:Oct 17, 2005
posts: 4988
votes: 12


@drhowarddrfine, the curly brace is there (I almost missed it too), it's just terribly formatted code making it hard to read. Here is the same code as above formatted for readability:

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

Junior Member

10+ Year Member

joined:Mar 9, 2006
posts: 177
votes: 0


Thank you @Fotiman. Yes, the code you posted above is what I am using and is not redirecting. Any suggestions?
2:53 pm on Jan 8, 2014 (gmt 0)

Senior Member from US 

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

joined:Oct 17, 2005
posts: 4988
votes: 12


Yes, see my first post. :)

Start by removing that HTML comment line.
Then, where is the GetCookie function defined?


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

Junior Member

10+ Year Member

joined:Mar 9, 2006
posts: 177
votes: 0


I define it on the mobile site. I clear my cookies and cache on my phone and still will not redirect.
3:17 pm on Jan 8, 2014 (gmt 0)

Senior Member from US 

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

joined:Oct 17, 2005
posts: 4988
votes: 12


Let me rephrase.
Is the function "GetCookie" defined in code that appears BEFORE this script block? I'm not asking about the cookie itself, I'm asking about the function that you're using to get the cookie.
For debugging purposes, maybe you could try something like this:

<script type="text/javascript">
if (screen.width <= 699) {
alert("screen.width is " + screen.width);
var mobilec = GetCookie('mobileredirect');
alert("mobilec = " + mobilec);
alert("mobilec!='mobi'? " + (mobilec!='mobi'));
if (mobilec!='mobi') {
alert('Redirecting...');
window.location = ("http://m.sample.com");
}
}
</script>
1:09 am on Jan 9, 2014 (gmt 0)

Junior Member

10+ Year Member

joined:Mar 9, 2006
posts: 177
votes: 0


Thanks Fotiman. I am getting closer. I need to just have the visitor redirected instead of alerted.
2:07 am on Jan 9, 2014 (gmt 0)

Senior Member from US 

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

joined:Apr 9, 2011
posts:13218
votes: 348


The alert is just for debugging. It's a quick-and-easy way to test your code. I also suspect that the function GetCookie began life at w3schools [w3schools.com] or similar. It seemed awfully familiar ;)
2:59 am on Jan 10, 2014 (gmt 0)

Junior Member

10+ Year Member

joined:Mar 9, 2006
posts: 177
votes: 0


Realized that I have to set the cookie differently in Wordpress. I added the following code to functions.php
function set_mobile_cookie() {
if (!isset($_COOKIE['mycookie']))
{ setcookie("mycookie", myvalue, time()+3600, "/", ".url.com");
}}
add_action( 'init', 'set_mobile_cookie');


I added the following code to the site's main index (not on WP):

<script language="JavaScript" type="text/javascript"> 
function checkCookie()
{
var mobilename=getCookie(mycookie);
if (mobilename!="myvalue")
{
if (screen.width <= 699) {
window.location = "http://m.url.com";
} } }
window.onload = checkCookie;

</script>


However, it is still not redirecting on index page. Any help appreciated.
3:13 pm on Jan 21, 2014 (gmt 0)

Senior Member from US 

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

joined:Oct 17, 2005
posts: 4988
votes: 12


You still never answered my question, so I will ask it again.

Is the function "GetCookie" defined in code that appears BEFORE this script block?

Somewhere in your code you must have something like this:

function GetCookie(...) {
...
}

Post that code. Also, make sure that function is defined prior to your code attempting to use it.
8:32 pm on Jan 21, 2014 (gmt 0)

Junior Member

10+ Year Member

joined:Mar 9, 2006
posts: 177
votes: 0


It is in the same script block.
8:51 pm on Jan 21, 2014 (gmt 0)

Administrator

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Aug 10, 2004
posts:10563
votes: 15


Mod's note: changed Java to JavaScript in the title.
10:03 pm on Jan 21, 2014 (gmt 0)

Senior Member from US 

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

joined:Oct 17, 2005
posts: 4988
votes: 12


Does it appear above or below this line?

window.onload = checkCookie;

I'm basically just trying to verify that GetCookie is defined and is returning a value. If GetCookie wasn't defined, then your script would error and quit and you would not get redirected.
10:04 pm on Jan 21, 2014 (gmt 0)

Senior Member from US 

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

joined:Oct 17, 2005
posts: 4988
votes: 12


Side note, it would be easier to help you debug this if all of the code being called was shown here. Without seeing GetCookie, it's difficult to provide useful feedback.
5:06 am on Jan 22, 2014 (gmt 0)

Junior Member

10+ Year Member

joined:Mar 9, 2006
posts: 177
votes: 0


I asked to delete other post.

<script language="JavaScript" type="text/javascript"> 
var mobilename=getCookie(mycookie);
function checkCookie()
{
if (mobilename!="myvalue") && (screen.width <= 699)
{window.location = "http://m.url.com"; }
}
window.onload = checkCookie;

</script>
5:08 am on Jan 22, 2014 (gmt 0)

Junior Member

10+ Year Member

joined:Mar 9, 2006
posts: 177
votes: 0


<script language="JavaScript" type="text/javascript"> 
var mobilename=getCookie(mycookie);
function checkCookie()
{
if ((mobilename!="myvalue") && (screen.width <= 699))
{window.location = "http://m.url.com"; }
}
window.onload = checkCookie;

</script>
5:13 am on Jan 22, 2014 (gmt 0)

Senior Member from US 

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

joined:Oct 17, 2005
posts: 4988
votes: 12


I still don't see a function named getCookie defined.
5:19 am on Jan 22, 2014 (gmt 0)

Junior Member

10+ Year Member

joined:Mar 9, 2006
posts: 177
votes: 0


Ok, I need help with that. How would I define it? I thought it was the following?

var mobilename=getCookie(mycookie);
5:36 am on Jan 22, 2014 (gmt 0)

Junior Member

10+ Year Member

joined:Mar 9, 2006
posts: 177
votes: 0


I have the my cookie set on the mobile site. this is for the desktop site. I am checking to see if the cookie is set yet and if it is not it will redirect. However, it is not redirecting on my cell (and javascript is on).
8:33 am on Jan 22, 2014 (gmt 0)

Senior Member from US 

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

joined:Apr 9, 2011
posts:13218
votes: 348


How would I define it? I thought it was the following?

var mobilename=getCookie(mycookie);

No, that's setting the value of the variable "mobilename". In this line, the variable is set to whatever value is returned by the function getCookie when given the parameter mycookie. (Which, come to think of it, also doesn't seem to have been defined anywhere.)

In the code snippet that fotiman posted up above

var mobilec = GetCookie('mobileredirect');
alert("mobilec = " + mobilec);


what content do you get in the alert box?
4:09 pm on Jan 22, 2014 (gmt 0)

Senior Member from US 

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

joined:Oct 17, 2005
posts: 4988
votes: 12


Also note, JavaScript is case sensitive. In your earlier example, you used GetCookie, while in your last example you used getCookie. JavaScript will treat those as 2 different functions (neither of which seems to be defined anywhere).
4:09 pm on Jan 22, 2014 (gmt 0)

Senior Member from US 

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

joined:Oct 17, 2005
posts: 4988
votes: 12


Also note, JavaScript is case sensitive. In your earlier example, you used GetCookie, while in your last example you used getCookie. JavaScript will treat those as 2 different functions (neither of which seems to be defined anywhere).
5:30 am on Jan 21, 2014 (gmt 0)

Junior Member

10+ Year Member

joined:Mar 9, 2006
posts: 177
votes: 0



System: The following 5 messages were spliced on to this thread from: http://www.webmasterworld.com/javascript/4638885.htm [webmasterworld.com] by whoisgregg - 11:32 am on Jan 22, 2014 (est -5)


I have the following code but it will not redirect if no cookie. Any help is appreciated.


<script language="JavaScript" type="text/javascript">
function checkCookie()
{
var mobilename=getCookie(mycookie);
if (mobilename!="myvalue")
{
if (screen.width <= 699) {
window.location = "http://m.url.com";
} } }
window.onload = checkCookie;

</script>
6:12 am on Jan 21, 2014 (gmt 0)

Administrator

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Aug 10, 2004
posts:10563
votes: 15


add some else if logic to handle when mobilename is null.
7:06 am on Jan 21, 2014 (gmt 0)

Senior Member from US 

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

joined:Apr 9, 2011
posts:13218
votes: 348


<tangent>
if (mobilename!="myvalue")
{
if (screen.width <= 699)

Seems to me that checking screen width is less work for the browser than looking for a cookie, so shouldn't the nesting be reversed?
</tangent>
5:35 am on Jan 23, 2014 (gmt 0)

Junior Member

10+ Year Member

joined:Mar 9, 2006
posts: 177
votes: 0


I get no alert….


<script language="JavaScript" type="text/javascript"> 
var mobilename=ReadCookie(mycookie);
if(mobilename.length == 0) {mobilename='newvisitor'; }

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

</script>
9:06 am on Jan 23, 2014 (gmt 0)

Senior Member from US 

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

joined:Apr 9, 2011
posts:13218
votes: 348


I get no alert….

That means the code isn't executing at all. Otherwise you'd get a blank alert box, or one containing a depressing word such as "undefined". Been there. Done that.

javascript is not the most complex language in the world, but it does have one thing in common with scary grownup programming languages: If it meets something it can't handle, it slams to a halt and refuses to utter another word.

This is the point where you keep moving the "alert(blahblah)" line further and further back in the code in hopes that it will eventually pop up. If it doesn't, it means there's a fatal error, such as a mismatched bracket or extraneous parenthesis, or a call to an undeclared function.
2:55 pm on Jan 23, 2014 (gmt 0)

Senior Member from US 

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

joined:Oct 17, 2005
posts: 4988
votes: 12



I get no alert….

And here is why:
Your JavaScript starts to get processed:

var mobilename=ReadCookie(mycookie);

Neither the function ReadCookie or the value you're passing to it, mycookie have been defined. This will cause an error, which will immediately end your script.

GetCookie
getCookie
ReadCookie

None of these functions have been defined in any of the code you've supplied so far.
This 36 message thread spans 2 pages: 36
 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members