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.
1 Answer
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>
-
Thanks @Lee_MSFT. The code definitely helped out. Although I replaced the line '$('a[href="/cmu"]').attr('href', "google.com/");' with location.href = "url". Appreciate it! Commented May 27, 2020 at 16:06