"Must-Visit" JavaScript

Discussion in 'Software' started by LadyYepperz, Oct 2, 2008.

  1. LadyYepperz

    LadyYepperz Private E-2

    Please help me with this script. I've tried repeatedly to get this script to work. If a sub-page (page two) on my site is linked directly, this script is suppose to redirect the browser to the home page (page one). But when page two IS opened from the link on page one, instead of showing page two, it directs the browser back to page one. :confused: Still with me?

    ************************************​

    Script Name: Must Visit From...

    Website URL: http://javascript.internet.com/user-details/must-visit-from.html

    Description: If other sites links directly the sub-pages of your site or if previous required pages are being skipped, you can now require your visitors to come from a certain page! Those that did not come from the required page are alerted then sent back to the required previous page. Clever!


    <!-- ONE STEP TO INSTALL MUST VISIT FROM....:

    1. Copy the coding into the HEAD of your HTML document -->

    <!-- STEP ONE: Paste this code into the HEAD of your HTML document -->

    <HEAD>

    <SCRIPT LANGUAGE="JavaScript">


    <!-- Begin
    var requiredfrom = "index.html"; // required prev. page
    if (document.referrer.indexOf(requiredfrom) == -1) {
    alert("You must come to this page from " + requiredfrom);
    window.location=requiredfrom;
    }
    // End -->
    </script>

    ************************************​

    I wonder...

    1. Does it matter the the link from the home page is inside an iframe?
    I've tried directing the script to reference the home page &
    the page containing the link to page two. Neither worked.

    2. Does it also matter that page two opens up in a new window?


    Thanks in advance for any help.
     
  2. PC-XT

    PC-XT Master Sergeant

    referrer doesn't work sometimes; it can be turned off. Also make sure requiredfrom is set correctly. I would have the full path from the domain in it so that someone can't make a page on another site with the same name and link to yours. You don't need the protocol, (the part up to :// before the domain starts) which is not part of the identification, but how it's used. This doesn't make a difference unless you provide different forms of the page, like an optional secure version. If it is just a regular http webpage, you can probably just copy from the browser. I would also use document.referrer.toLowerCase().indesOf(requiredfrom) instead, [adding .toLowerCase()] because domains are not case-sensitive, and you probably don't have another file with the same name but different letter case.
     
  3. LadyYepperz

    LadyYepperz Private E-2

    Sorry for the long delay in replying... Life demanded my attention.


    You wouldn't happen to have or know where i could find a working redirect script would you?

    This is simply the example as it was posted on a free script site.

    I tried your suggestion as well, with no success. :cry

    Someone told me this script wont run inside frames...
    Do you think this might be my problem?
     
  4. PC-XT

    PC-XT Master Sergeant

    It could be a frame problem. Frames usually pass the referrer of the page that was previously in that frame. I don't know if it passes the document that makes the frame to the first page, or an empty string. It may depend on the browser? You could set a session cookie on the main page, which would tell you if the person had visited it. You can make it a session cookie, where it is lost when the browser closes, or give it a specific expiration time. If the page doesn't find the cookie, it would go back to the main page. As for a redirection script, setting location.href like the example you gave is the usual. If you want to keep the visitor from just clicking the back button to get back, you can try the location.replace(url) method. If you want, you can also add a meta redirect (meta refresh with low or 0 time value) in the head area.

    BTW, someone can circumvent all this and get in anyway if they turn off JavaScript, but most people don't. If you use server-side scripting instead, they can circumvent it if they can make the browser send the proper fake referrer or cookie (which most people don't know how to do, or don't bother), although there are ways to validate the cookies. If the page is less useful for someone without JavaScript, they often turn it on to get the good content anyway.
     
  5. PC-XT

    PC-XT Master Sergeant

    I just thought I'd mention, if you want to redirect a page from inside a frame on that page, you can use parent.location instead of just location in the code. I think you can also check parent.referrer, though I'm not sure it would help.
     
  6. LadyYepperz

    LadyYepperz Private E-2

    Thanks for the help! Ive found an alternative to trying to use that script.

    And hopefully my hair will grow back soon in the places i pulled it out by the handfulls.
     

MajorGeeks.Com Menu

Downloads All In One Tweaks \ Android \ Anti-Malware \ Anti-Virus \ Appearance \ Backup \ Browsers \ CD\DVD\Blu-Ray \ Covert Ops \ Drive Utilities \ Drivers \ Graphics \ Internet Tools \ Multimedia \ Networking \ Office Tools \ PC Games \ System Tools \ Mac/Apple/Ipad Downloads

Other News: Top Downloads \ News (Tech) \ Off Base (Other Websites News) \ Way Off Base (Offbeat Stories and Pics)

Social: Facebook \ YouTube \ Twitter \ Tumblr \ Pintrest \ RSS Feeds