I am currently trying to retrieve the current URL of a document library using JavaScript.
My ultimate goal is to un-hide a web part when the user clicks a specific folder.
Right now,
My code so far:
ExecuteOrDelayUntilScriptLoaded(loadFolderView, "sp.js");
function unhidepart() {
var unhideWP = document.getElementById("MSOZoneCell_WebPartWPQ1");
unhideWP.style.display="block";
}
function hidepart() {
var hideWP = document.getElementById("MSOZoneCell_WebPartWPQ1");
hideWP.style.display="none";
}
function loadFolderView() {
hidepart();
var folderUrl = "https://example.com/sites/doclib/Forms/AllItems.aspx?RootFolder=%2Fsites%2Fdoclib%2FFOLDER%20NAME";
var ctx = new SP.ClientContext.get_current();
var site = ctx.get_site();
ctx.load(site);
ctx.executeQueryAsync(
function () {
var url = site.get_url();
console.log(url);
if (url == folderUrl) {
unhidepart();
}
},
function (sender, args) {
console.log('error');
});
}
Right now it only logs /sites/doclib
and I'm not sure how to get the full URL of the folder.
Any help or suggestions to make this easier would be very appreciated.
var pageUrl = window.location.href;
. does this work for you?