Create an Exit link which returns users to referring page

Snehal Rana
Snehal Rana used Ask the Experts™
on
We are using SharePoint 2007. The users come to my site from a link setup on another site. I need to create an Exit link on the header of my main Site collection. When user clicks on this Exit link they should be send to the site or page they came from.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Here is how I would accomplish this using JavaScript:

1) Get the referring URL (document.referrer)

2) Set a cookie with the the referring URL (here is info on how to set a cookie in JavaScript: http://www.quirksmode.org/js/cookies.html)

3) Create a function that enables the Exit link if a referring URL cookie is set

4) Set the link to call a JavaScript function that reads the cookie and redirects the user to the referred page.

Ill write up a tutorial for this, but just wanted to put this out there so that you had somewhere to start in the meantime.
Ok, so here is my quick how to on this.  I used jQuery and the jQuery cookie plugin (http://github.com/carhartl/jquery-cookie/raw/master/jquery.cookie.js).

In this example, I set the referring URL cookie if there was no cookie already set (so that it doesn't get reset for every page the user visits).  Then the ExitURL() function simply reads the cookie, null's the cookie out and redirects.



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
    <script src="Scripts/jquery-1.4.1-vsdoc.js" type="text/javascript"></script>
    <script src="Scripts/jquery.cookie.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            var referralCookie = $.cookie('referralURL');
            if (referralCookie == null) {
                referralCookie = document.referrer;
                $.cookie('referralURL', referralCookie, { expires: 2 });
            } 
        });

        function ExitURL() {
            var referralCookie = $.cookie('referralURL');
            $.cookie('referralURL', null);
            window.location.replace(referralCookie);
        }
     </script>
</head>
<body>
<a id="exitURL" href="#" onclick="ExitURL()">Exit</a>
</body>
</html>

Open in new window

Exploring SQL Server 2016: Fundamentals

Learn the fundamentals of Microsoft SQL Server, a relational database management system that stores and retrieves data when requested by other software applications.

Snehal RanaSenior SharePoint Consultant

Author

Commented:
I am testing this out as I write. Appreciate quick response.
Snehal RanaSenior SharePoint Consultant

Author

Commented:
Hi Chapmanjw,

I tried inserting this code on default.aspx and it errors out as below:
" '$' is undefined"

Let me know
Snehal RanaSenior SharePoint Consultant

Author

Commented:
Excellent solution by chapmanjw. Got this working with little tweak. I needed to place both the js files in appropriate location and it worked like a charm.
Thanks a bunch

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial