I want to be able to go from one page to another in sharepoint, and get the value of the previous page that the user came from. I investigated the method called: SP.Utilities.HttpUtility.navigateTo, but it doesn't give me anything related to the previous page. Is it a good practice to use cookies? or Sessionstorage? Pleas note I am working on office 365 edition of sharepoint
2 Answers
The navigateTo function won't give you any info about the previous page. You should not use sessionStorage or anything of that kind for this purpose. You must use the function: "GoToPage"
which is available out of the box in SharePoint pages, it takes 2 parameters, the second is optional. The first parameter is the page's path where you want to be redirected, the second parameter states that if you have already a "source" in the query string, so you can use it. To make it simple for you, you can just omit this parameter and use the function like:
GoToPage("http://mypage.com")
, this will redirect you to the page and it's going to look like: http://mypage.com?source=http://mypreviouspage
.
Now you can easily read the query string for the previous page.
But the previous page can be anything. Maybe you clicked a Favourite or a direct link in an email.
That is the way the web works. All you got is document.referrer
So if you want to pass data you have to manage it yourself with cookies (kinda oldschool) or SessionStorage.
There is no best practice, it depends on what you want to do.
Cookies and SessionStorage are tied to the browser, if you switch Browser or log in with a different device there is no state available. You could store information in a List.
If you can built the URL on that previouspage you could also use URL parameters.
Note: SharePoint provides several ways of getting at those params: What does this code getQueryStringParameter do?