5

In SharePoint how do you get the ID value from the url of the page you are on using javascript( Jquery,ajax, Rest..), e.g. I have the url like so: http:// ddd/mypage?ID=xx I tried before to retrieve the full url but it didn't get the ID and its value. so I have to get the ID value directly with the get method

1
  • Ok, so in order to make this work where do I enter the code listed above? - At the end of the page URL? - In a Script Editor web part? - Other? Jan 29, 2021 at 23:11

4 Answers 4

15

you can use SharePoint’s GetUrlKeyValue() function to get item id. Example:

var itemId = GetUrlKeyValue("ID", false, location.href);
1
  • Interesting, so many built in functions. One could also use: JSRequest.EnsureSetup(); JSRequest.QueryString["ID"] May 20, 2014 at 20:27
6

Use the following javascript function :

function getParameterByName(name) {  
    name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
    var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"), results = regex.exec(location.search);
    return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}
var prodId = getParameterByName('prodId');
0
  function getParameterByName(name, url) {
                if (!url) url = window.location.href;
                name = name.replace(/[\[\]]/g, "\\$&");
                var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
                    results = regex.exec(url);
                if (!results) return null;
                if (!results[2]) return '';
                return decodeURIComponent(results[2].replace(/\+/g, " "));
            }

//ID Get//

var mid = getParameterByName("ID", window.location.href);
-1

Problem resolved. Thanks for your contribution.

There is the code to get the ID value:

var query = window.location.search.substring(1);
var vars = query.split("=");
var ID= vars[1];
2
  • 2
    The problem with this solution is it assumes the ID is the first and ONLY parameter specified in the query string. Please use Aanchal's or Naddeem's solution instead.
    – Rob D'Oria
    May 20, 2014 at 15:46
  • 1
    but this solution work for me, because the only paramter is the ID, so for Aanchal's or Naddeem's solution instead, it dosen't work for me, and i find this solution easy to use
    – Issibra
    May 22, 2014 at 9:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.