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

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

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

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');
  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);
up vote -1 down vote accepted

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
    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 '14 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 '14 at 9:14

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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