homepage Welcome to WebmasterWorld Guest from 54.196.168.78
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>

 

drhowarddrfine




msg:4635922
 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




msg:4635923
 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




msg:4635925
 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




msg:4635927
 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




msg:4635928
 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




msg:4635929
 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




msg:4635936
 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




msg:4636061
 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




msg:4636069
 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




msg:4636301
 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




msg:4638965
 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




msg:4639048
 8:32 pm on Jan 21, 2014 (gmt 0)

It is in the same script block.

phranque




msg:4639066
 8:51 pm on Jan 21, 2014 (gmt 0)

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

Fotiman




msg:4639089
 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




msg:4639091
 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




msg:4639171
 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




msg:4639172
 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




msg:4639174
 5:13 am on Jan 22, 2014 (gmt 0)

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

weddingm




msg:4639175
 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




msg:4639178
 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




msg:4639204
 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




msg:4639322
 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




msg:4639323
 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




msg:4638887
 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




msg:4638890
 6:12 am on Jan 21, 2014 (gmt 0)

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

lucy24




msg:4638895
 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




msg:4639509
 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




msg:4639545
 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




msg:4639611
 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