0

Not specifically a SharePoint issue but i'm using it with the confines of SharePoint 2013 REST API. The following code is used to get URL parameters.

<script type="text/javascript">
function getUrlVars() {
var vars = {};
var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
vars[key] = value;
});
return vars;
}
</script>

<script type="text/javascript">
var url = window.location.protocol + "//" + window.location.hostname;
</script>

The page with this code is accessed from a link on another page. Within that link i'm passing in the list item ID and also the list item Title. When the page with the above code loads the Title in the URL has %20 in place of all spaces. I've read up on this and found i need to use decodeURI or decodeURIComponent. I've tried to do this in a number of places and alerted the result in the browser but i can't seem to get rid of the %20 from the title within the URL so its obvious i'm not doing it in the right place.

this is my result....

http://PAGE URL HERE/NewsArchive.aspx?Story=New%20site%20launched&ID=17

Where should i use the decodeURI? Is it on the page with the link that contains the list item title and ID or is it somewhere within the above code?

Any help appreciated.

0

your should first decode your url and then apply .split/.replace logic. you should write something like this:

var parts = decodeURIComponent(window.location.href.toString());
3
  • I'm afraid my level of understanding isn't as simple just doing what you suggested. I'm sure it's right, but i'm not sure how/where to implement this. Could you provide a bit more context as to where that line should go and then what i should do afterwards?
    – Ian
    Jun 21 '17 at 13:03
  • put this code just after declaration of var vars={}; then do something like this var parts2 = parts.replace(.....and so on
    – DvG
    Jun 21 '17 at 13:09
  • Whatever i seem to try isn't working. I know i'm doing it wrong, and i know you folks don't like to do peoples jobs for them, but i can't quite get there. Here is what i'm trying.... function getUrlVars() { var vars = {}; var parts = decodeURIComponent(window.location.href.toString()); alert(parts); var parts1 = parts.replace(/%20/g, "-"); alert(parts1); var parts2 = parts1.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) { vars[key] = value; }); return vars; }
    – Ian
    Jun 21 '17 at 13:41

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.