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, a distinguished professional, boasts an impressive track record as a Microsoft MVP, having achieved this prestigious recognition for the eighth consecutive year since 2015. With an extensive career spanning over 18 years, Adnan has honed his expertise in various domains, notably excelling in SharePoint, Microsoft 365, Microsoft Teams, the .Net Platform, and Microsoft BI. Presently, he holds the esteemed position of Senior Microsoft Consultant at Olive + Goose. Notably, Adnan served as the MCT Regional Lead for the Pakistan Chapter from 2012 to 2017, showcasing his leadership and commitment to fostering growth within the tech community. His journey in the realm of SharePoint spans 14 years, during which he has undertaken diverse projects involving both intranet and internet solutions for both private and government sectors. His impact has transcended geographical boundaries, leaving a mark on projects in the United States and the Gulf region, often collaborating with Fortune 500 companies. Beyond his roles, Adnan is a dedicated educator, sharing his insights and knowledge as a trainer. He also passionately advocates for technology, frequently engaging with the community through speaking engagements in various forums. His multifaceted contributions exemplify his dedication to the tech field and his role in driving its evolution.

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.