In SharePoint, we use custom client side script (jQuery) to load or submit content on a page, some time we also need to get few values from Query string. There are multiple ways of doing that, here I am going to share a function to which you just need to pass the query string field and it will return the value.
function getQueryStringParameterByName(parameterName) {
parameterName = parameterName.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
var regex = new RegExp("[\\?&]" + parameterName + "=([^&#]*)"), results = regex.exec(location.search);
return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}
The function call is very simple. For example you on a page having URL https://mstechtalk.sharepoint.com/Lists/Tasks/EditForm.aspx?List=9196139c-84a6-4d83-b375-c94e861c7637&ID=1793&Web=48b8f4ce-c0a6-4d81-a385-667c9a80cba5&PageView=Shared&InitialTabId=Ribbon.WebPartPage&VisibilityContext=WSSWebPartPage
Now you have to get the List item id, you can the ID query string in above URL, simply call the function to get the specific query string.
var itemId = getQueryStringParameterByName('ID');
Console.log('Item ID: ' + itemId)
alert('Item ID: ' + itemId)
Leave a Reply
You must be logged in to post a comment.






2 replies on “Get Query String parameters using JavaScript”
Hi there,I read your blogs named “Get Query String parameters using JavaScript” on a regular basis.Your story-telling style is witty, keep up the good work! And you can look our website about اغاني.
Fantastic Blog. Really enjoyed reading.