Skip to content

Instantly share code, notes, and snippets.

@orcaman
Created May 20, 2015 12:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save orcaman/80fde279584d9251caca to your computer and use it in GitHub Desktop.
Save orcaman/80fde279584d9251caca to your computer and use it in GitHub Desktop.
encode url params for unsafe url
function prep(url) {
var s = url.split('?');
if (s.length < 1) { // no qs for this url
return s;
}
var qsPart = s[1];
var params = qsPart.split('&');
var result = s[0] + '?'; // url part +
for (var i = 0; i < params.length; i++) {
var param = params[i];
var paramSections = param.split('=');
if (paramSections.length > 1) {
var paramName = paramSections[0];
var paramValue = paramSections[1];
result += paramName + '=' + encodeURIComponent(paramValue) + '&';
} else {
continue;
}
}
if (result.lastIndexOf('&') === result.length - 1) {
result = result.substr(0, result.length - 1);
}
return result;
}
console.log(prep('http://vast.bp3863356.btrll.com/vast/3863356?n=1426410433&br_w=300&br_h=250&br_pageurl=http://localhost:9000/app/#/flash&br_conurl=http://sdk.streamrail.com/demos/debugger/nasa_is_with_you_when_you_fly.mp4'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment