homepage Welcome to WebmasterWorld Guest from 54.227.215.139
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
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

5+ Year Member



 
Msg#: 4635874 posted 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>

 

drhowarddrfine

WebmasterWorld Senior Member 5+ Year Member



 
Msg#: 4635874 posted 2:32 pm on Jan 8, 2014 (gmt 0)

Java is not the same thing as JavaScript.

You are missing a curly brace after the first var mobilec = ....

Fotiman

WebmasterWorld Senior Member fotiman us a WebmasterWorld Top Contributor of All Time 5+ Year Member



 
Msg#: 4635874 posted 2:35 pm on Jan 8, 2014 (gmt 0)

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]

Fotiman

WebmasterWorld Senior Member fotiman us a WebmasterWorld Top Contributor of All Time 5+ Year Member



 
Msg#: 4635874 posted 2:38 pm on Jan 8, 2014 (gmt 0)

@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>

weddingm

5+ Year Member



 
Msg#: 4635874 posted 2:48 pm on Jan 8, 2014 (gmt 0)

Thank you @Fotiman. Yes, the code you posted above is what I am using and is not redirecting. Any suggestions?

Fotiman

WebmasterWorld Senior Member fotiman us a WebmasterWorld Top Contributor of All Time 5+ Year Member



 
Msg#: 4635874 posted 2:53 pm on Jan 8, 2014 (gmt 0)

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>

weddingm

5+ Year Member



 
Msg#: 4635874 posted 2:57 pm on Jan 8, 2014 (gmt 0)

I define it on the mobile site. I clear my cookies and cache on my phone and still will not redirect.

Fotiman

WebmasterWorld Senior Member fotiman us a WebmasterWorld Top Contributor of All Time 5+ Year Member



 
Msg#: 4635874 posted 3:17 pm on Jan 8, 2014 (gmt 0)

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>

weddingm

5+ Year Member



 
Msg#: 4635874 posted 1:09 am on Jan 9, 2014 (gmt 0)

Thanks Fotiman. I am getting closer. I need to just have the visitor redirected instead of alerted.

lucy24

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



 
Msg#: 4635874 posted 2:07 am on Jan 9, 2014 (gmt 0)

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 ;)

weddingm

5+ Year Member



 
Msg#: 4635874 posted 2:59 am on Jan 10, 2014 (gmt 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.

Fotiman

WebmasterWorld Senior Member fotiman us a WebmasterWorld Top Contributor of All Time 5+ Year Member



 
Msg#: 4635874 posted 3:13 pm on Jan 21, 2014 (gmt 0)

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.

weddingm

5+ Year Member



 
Msg#: 4635874 posted 8:32 pm on Jan 21, 2014 (gmt 0)

It is in the same script block.

phranque

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



 
Msg#: 4635874 posted 8:51 pm on Jan 21, 2014 (gmt 0)

Mod's note: changed Java to JavaScript in the title.

Fotiman

WebmasterWorld Senior Member fotiman us a WebmasterWorld Top Contributor of All Time 5+ Year Member



 
Msg#: 4635874 posted 10:03 pm on Jan 21, 2014 (gmt 0)

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.

Fotiman

WebmasterWorld Senior Member fotiman us a WebmasterWorld Top Contributor of All Time 5+ Year Member



 
Msg#: 4635874 posted 10:04 pm on Jan 21, 2014 (gmt 0)

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.

weddingm

5+ Year Member



 
Msg#: 4635874 posted 5:06 am on Jan 22, 2014 (gmt 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>

weddingm

5+ Year Member



 
Msg#: 4635874 posted 5:08 am on Jan 22, 2014 (gmt 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>

Fotiman

WebmasterWorld Senior Member fotiman us a WebmasterWorld Top Contributor of All Time 5+ Year Member



 
Msg#: 4635874 posted 5:13 am on Jan 22, 2014 (gmt 0)

I still don't see a function named getCookie defined.

weddingm

5+ Year Member



 
Msg#: 4635874 posted 5:19 am on Jan 22, 2014 (gmt 0)

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

var mobilename=getCookie(mycookie);

weddingm

5+ Year Member



 
Msg#: 4635874 posted 5:36 am on Jan 22, 2014 (gmt 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).

lucy24

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



 
Msg#: 4635874 posted 8:33 am on Jan 22, 2014 (gmt 0)

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?

Fotiman

WebmasterWorld Senior Member fotiman us a WebmasterWorld Top Contributor of All Time 5+ Year Member



 
Msg#: 4635874 posted 4:09 pm on Jan 22, 2014 (gmt 0)

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).

Fotiman

WebmasterWorld Senior Member fotiman us a WebmasterWorld Top Contributor of All Time 5+ Year Member



 
Msg#: 4635874 posted 4:09 pm on Jan 22, 2014 (gmt 0)

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).

weddingm

5+ Year Member



 
Msg#: 4635874 posted 5:30 am on Jan 21, 2014 (gmt 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>

phranque

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



 
Msg#: 4635874 posted 6:12 am on Jan 21, 2014 (gmt 0)

add some else if logic to handle when mobilename is null.

lucy24

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



 
Msg#: 4635874 posted 7:06 am on Jan 21, 2014 (gmt 0)

<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>

weddingm

5+ Year Member



 
Msg#: 4635874 posted 5:35 am on Jan 23, 2014 (gmt 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>

lucy24

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



 
Msg#: 4635874 posted 9:06 am on Jan 23, 2014 (gmt 0)

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.

Fotiman

WebmasterWorld Senior Member fotiman us a WebmasterWorld Top Contributor of All Time 5+ Year Member



 
Msg#: 4635874 posted 2:55 pm on Jan 23, 2014 (gmt 0)


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 ( [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