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)

 

Adnan is two time SharePoint MVP (Most Valuable Professional) with over 12 years of extensive experience with major expertise on SharePoint Server, Office 365, .Net Platform and Microsoft BI. He is currently working SharePoint Architect at FMT Consultants. He is MCT Regional Lead for Pakistan Chapter since 2012. He is working on SharePoint for past nine years and worked on different intranet/intranet solutions for private & govt. sector majorly in Gulf region, which include OOB and customized solutions. He is a trainer, technology evangelist and also speaks in community forums.

Leave a Reply

2 replies on “Get Query String parameters using JavaScript”

  • August 27, 2017 at 11:27 pm

    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 اغاني.

  • September 2, 2017 at 4:53 am

    Fantastic Blog. Really enjoyed reading.