2

I am using SharePoint Online and have inserted a Script editor webpart with the following code to redirect from page - "SPage" to page "Pages". However it is not working, can anyone provide me assistance in getting this to work?

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript">
if(location.href === 'https://mysite.sharepoint.com/sites/ABC/SitePages/SPage.aspx'){
    location.replace ("https://mysite.sharepoint.com/sites/ABC/SitePages/Pages.aspx");
    return False;
}
</script>
1
  • For future reference, "it is not working" is not very helpful. Please explain how it is not working.
    – wjervis
    Nov 17, 2016 at 13:38

3 Answers 3

6

Try it as below:

if(location.href === 'https://mysite.sharepoint.com/sites/ABC/SitePages/SPage.aspx')
{
     location.href = "https://mysite.sharepoint.com/sites/ABC/SitePages/Pages.aspx";
}
2
  • if we have multiple site collections under the same web collection, is it best practice to write this javascript in the site collection master page?
    – hkhan
    Jan 17, 2019 at 20:19
  • @hkhan yes, that would be the way to go if you want it across multiple site collections in a web app. Jan 18, 2019 at 6:27
3

first convert the LHS and RHS to lowercase and compare.

var spageUrl = 'https://mysite.sharepoint.com/sites/ABC/SitePages/SPage.aspx';
if(location.href.toLowerCase() === spageUrl.toLowerCase())
{
     location.href = "https://mysite.sharepoint.com/sites/ABC/SitePages/Pages.aspx";
}
1
  • Thanks for providing an answer Amit. I havent been able to check if it works or not as I used the solution provided by Gautam first and it was successfull for me
    – cr1
    Nov 17, 2016 at 6:29
1
var url = _spPageContextInfo.webServerRelativeUrl;
url = url.replace(/\/$/, '') + '/';
var check_URL = url + "SitePages/SPage.aspx";
var redirectURL = url + "SitePages/Pages.aspx";
if(location.href === check_URL)
{
    window.location = redirectURL;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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