Question

For example, a site in the global navigation, when clicked should redirect to another site for all users except one. Also, users who have saved the site as bookmarks should be redirected too when they open the link. How does the .js file look like and where should I add it? Any help appreciated.

Was it helpful?

Solution

You could update the link url when DOM loaded.

For example:

<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
    <script type="text/javascript">
        function GetCurrentUser() {            
            var userid = _spPageContextInfo.userId;
            var requestUri = _spPageContextInfo.webAbsoluteUrl + "/_api/web/getuserbyid(" + userid + ")";
            var requestHeaders = { "accept": "application/json;odata=verbose" };

            return $.ajax({
                url: requestUri,
                contentType: "application/json;odata=verbose",
                headers: requestHeaders
            });
        }
        $(function () {
            GetCurrentUser().done(function (data) {
                alert(data.d.Title);
                if (data.d.Title == "usera") {                    
                    $('a[href="/cmu"]').attr('href', "https://www.google.com/");
                }
            })            
        })        
    </script>

implement JavaScript in master page of SharePoint 2013

Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top