Log in

View Full Version : Javascript Redirect Option



Austin Klei
03-21-2010, 07:03 PM
I am wanting to make it where we use a cookie for a redirect. Example:

If i went to my website, it would take me to the homepage. There would be an button that on click, it would make you redirect to another page for the session, until you close your browser and reopen it. And when you come back, you could do the same thing.



How do i do this without server side scripting. I want to do it with javascript, or html, or whatever.

traq
03-21-2010, 08:11 PM
Do you mean that, after you click the button on your home page, you want to be redirected to a different page if you try to visit the home page again? Or just be redirected once, when you click on the button?

djr33
03-21-2010, 08:42 PM
Do you want a "bookmark" script in the sense that when you close your site the next time you go to your site it will redirect you to wherever you left off "reading"?

That sounds very easy: just store any page loaded to a cookie; then on loading any page for the first time*, set location.href of the page to the cookie's value.

*Here's the hard part: how do you know when to do this? Or perhaps you'll just do it every time they go to your home page.

Austin Klei
03-21-2010, 11:15 PM
Do you mean that, after you click the button on your home page, you want to be redirected to a different page if you try to visit the home page again? Or just be redirected once, when you click on the button?

The First thing you said. It would redirect you only for that session, over and over as long as you dont close your browser. (the program)

djr33
03-21-2010, 11:24 PM
That's fairly easy then, using serverside or clientside scripting. Using PHP you can use sessions that rely on a cookie set to expire after the "session" (a long time of inactivity or closing the program).
Using Javascript you can have also a "session" by using the same cookie settings-- the defaults. It will expire once the activity has ended.

setCookie() once you do the action, then just check the value of the cookie and see what it says when you click the link-- if it's not set, do the default, otherwise go to the "redirect" page.
Here's some more info on cookies: http://www.w3schools.com/JS/js_cookies.asp


If possible use a serverside backup or some of your guests won't be redirect properly.

(The extra serverside information is mostly for others who have similar questions about using this with PHP, etc. If this doesn't apply to you, don't worry about it.)

Austin Klei
03-21-2010, 11:27 PM
Could you code this for me? Please. :]

djr33
03-21-2010, 11:37 PM
You would probably benefit by working this out yourself, but here's a very rough (untested) version:

Your link:
<a href="DEFAULT.htm" onClick="myredirect();" ?>

In the head section of your page:

<script language="javascript">
function myredirect() {
if (getCookie('myredirect')=='1') { window.location="REDIRECT.htm"; }
else { setCookie('myredirect','1',1); }
}


////below here is just code to support that:
///borrowing this from the link in my last post:
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 "";
}
function setCookie(c_name,value,expiredays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate()+expiredays);
document.cookie=c_name+ "=" +escape(value)+
((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}
</script>

Austin Klei
03-21-2010, 11:50 PM
That code didnt work for me.


What i want it to do is after clicking a check box on a page, it will redirect you to another page for the rest of your session.

djr33
03-21-2010, 11:53 PM
As I said, untested. You should be able to use "onChange" for the checkbox using the same function, but you'll also want to be sure that the checkbox is checked (rather than changed to unchecked).

Austin Klei
03-22-2010, 12:04 AM
I'm not a good coder. Could you please code a working one? Thanks

djr33
03-22-2010, 12:10 AM
It would take me a while to work this out because Javascript is not my area of expertise-- I code mostly with PHP. If you want to try to figure it out yourself, someone can probably help you with that, or perhaps someone will come along later and offer to write the whole script. You could also ask for paid help if you need it sooner.