Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

JavaScript Forum


You are currently viewing our JavaScript Forum as a guest. Please register to participate.
Login



Reply
Redirect to different page if no cookie detected
Old 08-07-2009, 07:17 AM Redirect to different page if no cookie detected
Junior Talker

Posts: 3
Trades: 0
Hello,

I know very little with Javascript and was wondering if I could get some help. I'm looking to redirect to a different page on my site if no cookie is present--in particular, for certain browsers.

A pseudo code would be:

---------------------

Is user using IE, Firefox or Chrome?

......If yes, continue forward like normal.

......If no, check to see if cookie is present.

............Is cookie present?

..................If yes, continue forward like normal.

..................If no, redirect to alternate page.

........................On alternate page, at the end, have a line that says, "Proceed to <original link destination>?"

..............................If user clicks to proceed, create cookie for future reference as to not load this page again throughout entire site.

---------------------

Any help would be very appreciated!

Last edited by silverpen; 08-07-2009 at 07:21 AM..
silverpen is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 08-07-2009, 07:42 AM Re: Redirect to different page if no cookie detected
freeloader's Avatar
Experienced Talker

Posts: 35
Trades: 0
Code:
<script>
if(!(/(Firefox|MSIE|Chrome)/.test(navigator.userAgent)) &&
   !document.cookie) window.location.href = "alternate.page";
</script>
alternate page:
Code:
<a href="original.page" onclick="var d=new Date();d.setDate(d.getDate()+10000);document.cookie='msg=IwasHere; expires='+d.toGMTString()+';';">Proceed to original page</a>
[js cookie]
__________________
"It is a profitable thing, if one is wise, to seem foolish." --Aeschylus

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE

Last edited by freeloader; 08-07-2009 at 07:45 AM..
freeloader is offline
Reply With Quote
View Public Profile
 
Old 08-08-2009, 12:12 AM Re: Redirect to different page if no cookie detected
Junior Talker

Posts: 3
Trades: 0
This is terrific, thank you freeloader! I just need help with one more thing based upon this script:

This code will be used on every page throughout the site. As a result, I would need the alternate page's "Proceed to Original Page" URL to be able to know what URL it was originally going to (dynamically) before it was redirected to the alternate page.

I was thinking a PHP http_REFFER script with it echoing in the href would work but for some reason the it won't create the referring URL.

Any suggestions?
silverpen is offline
Reply With Quote
View Public Profile
 
Old 08-08-2009, 01:32 AM Re: Redirect to different page if no cookie detected
freeloader's Avatar
Experienced Talker

Posts: 35
Trades: 0
PHP Code:
<a href="<?php echo $_SERVER['HTTP_REFERER']; ?>
   onclick="var d=new Date(); d.setDate(d.getDate()+10000);
   document.cookie='msg=IwasHere; expires='+d.toGMTString()+';';
   window.history.go(-1); return false;">Proceed to original page</a>
__________________
"It is a profitable thing, if one is wise, to seem foolish." --Aeschylus

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE

Last edited by freeloader; 08-08-2009 at 06:37 AM..
freeloader is offline
Reply With Quote
View Public Profile
 
Old 08-09-2009, 03:21 AM Re: Redirect to different page if no cookie detected
Junior Talker

Posts: 3
Trades: 0
Thank you again freeloader. Works like a charm! May I ask for one last Javascript-based assistance? How can I get this script to work multiple times on the same onClick/onMouseover/etc.?

Code:
function setInnerText (elementId, text) {
var element;
if (document.getElementById) {
element = document.getElementById(elementId);
}
else if (document.all) {
element = document.all[elementId];
}
if (element) {
if (typeof element.textContent != 'undefined') {
element.textContent = text;
}
else if (typeof element.innerText != 'undefined') {
element.innerText = text;
}
else if (typeof element.removeChild != 'undefined') {
while (element.hasChildNodes()) {
element.removeChild(element.lastChild);
}
element.appendChild(document.createTextNode(text)) ;
}
}
}
For example, this works:
Code:
<span id="text_id">Text</span>
 
<span onClick="setInnerText(text_id, 'This will be new text');'">Text</span>
... but this does not work:

Code:
<span id="text_id">Text</span>
<span id="text_id2">Text</span>
<span id="text_id3">Text</span>
 
<span onClick="setInnerText(text_id, 'This will be new text'); 
setInnerText(text_id2, 'This is more text'); 
setInnerText(text_id3, 'And ever more text');'">Text</span>
silverpen is offline
Reply With Quote
View Public Profile
 
Old 08-09-2009, 05:39 AM Re: Redirect to different page if no cookie detected
freeloader's Avatar
Experienced Talker

Posts: 35
Trades: 0
Code:
<span id="text_id1">Text</span>
<span id="text_id2">Text</span>
<span id="text_id3">Text</span>
<script>
function setInnerText(el, text) {
  document.getElementById(el).innerHTML = text;
}
</script>
<a onclick="setInnerText('text_id1', 'This will be new text'); 
setInnerText('text_id2', 'This is more text'); 
setInnerText('text_id3', 'And ever more text');">Link</a>
Note, ids passed to function are strings as well. getElementById() and innerHTML attribute is supported by all contemporary and future browsers. There was also one loose "'" at the end of onclick event. Above surely works.
__________________
"It is a profitable thing, if one is wise, to seem foolish." --Aeschylus

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE
freeloader is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Redirect to different page if no cookie detected
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.24184 seconds with 12 queries