Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: Javascript Redirect Option

  1. #1
    Join Date
    Feb 2010
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Javascript Redirect Option

    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.

  2. #2
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    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?

  3. #3
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    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.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  4. #4
    Join Date
    Feb 2010
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by traq View Post
    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)

  5. #5
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    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.)
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  6. #6
    Join Date
    Feb 2010
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Could you code this for me? Please. :]

  7. #7
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    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:
    Code:
    <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>
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  8. #8
    Join Date
    Feb 2010
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    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.

  9. #9
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    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).
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  10. #10
    Join Date
    Feb 2010
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    I'm not a good coder. Could you please code a working one? Thanks

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •