Using document.referrer I am able to get the previous site URL but I want to get the previous site title also. How to do this?

  • How are you getting redirected to the current site from the previous one? is it through a button/link click or something else? Also are you in position to add query string to the url? – Asad Refai Nov 17 '15 at 9:48
  • By clicking on link I am going to next site, so there I need to have previous site title – KumarV Nov 17 '15 at 9:53
  • On that link button, can you modify URL by adding a query string? I mean the href of link button is set by you in code? – Asad Refai Nov 17 '15 at 9:54
  • 1
    If you want to escape server calls to get title, then you can append _spPageContextInfo.webTitle as a query string parameter. And on Next Site you can retrieve using JSRequest.QueryString["YourQueryString"]. – Asad Refai Nov 17 '15 at 10:16
  • 1
    If using QueryString, beware of XSS, e.g., ?prevTitle=<script>alert("Peekaboo!")</script> – Jussi Palo Nov 17 '15 at 10:34

You can use JSOM for this.

function retrieveWebSite() {
    var clientContext = new SP.ClientContext(document.referrer);
    var oWebsite = clientContext.get_web();


    clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));

function onQuerySucceeded(sender, args) {
    alert('Title: ' + oWebsite.get_title());

function onQueryFailed(sender, args) {
    alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
| improve this answer | |

If you want to escape server calls to get Web Title, and are in position to set the URL of the Links which takes users to Next Site.

Then make use of an OOB object _spPageContextInfo which holds lots of important and much needed values, and Web Title is one of them. _spPageContextInfo.webTitle is the one we are interested in.

On Previous Site

The links on which when you click takes you to the Next Site, you can add query string into the HREF. Something like below

var webTitle = _spPageContextInfo.webTitle;
var urlToNextSite = 'http://YourNextSite.com?PreviousWebTitle=webTitle';

On Next Site

Using JSRequest we can retrieve the query string values.

var q = JSRequest.QueryString["PreviousWebTitle"]; 

Note: While using Query Strings, we need to be aware of XSS attacks

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.