Skip to content

Instantly share code, notes, and snippets.

@PSF1
Created April 26, 2018 14:07
Show Gist options
  • Save PSF1/807ef472ec18dc7c67239ef2ad2f2a09 to your computer and use it in GitHub Desktop.
Save PSF1/807ef472ec18dc7c67239ef2ad2f2a09 to your computer and use it in GitHub Desktop.
Javascript URL tools.
/**
* Tiny function for basic query string parsing.
*
* @see https://davidwalsh.name/query-string-javascript
*
* @param name
* Parameter name.
* @param url
* Url where search.
*
* @returns {string}
*/
function getUrlParameter(name, url) {
name = name.replace(/[\[]/, '\\[').replace(/[\]]/, '\\]');
var regex = new RegExp('[\\?&]' + name + '([=]?)([^&#]*)');
var results = regex.exec(url?url:location.search);
return results === null ? undefined : decodeURIComponent(results[2].replace(/\+/g, ' '));
};
/**
* Get all URL query parameters.
*
* @see http://www.designchemical.com/blog/index.php/jquery/8-useful-jquery-snippets-for-urls-querystrings/
* @see https://gist.github.com/jlong/2428561
*
* @param url
* The url.
* @returns {Object}
* Parameters list.
*/
function getQueryParameters(url) {
// If not url defined, use the current one.
if (!url) {
url = document.URL;
}
var parser = document.createElement('a');
parser.href = url;
// http://example.com:3000/pathname/?search=test#hash
// parser.protocol; // => "http:"
// parser.hostname; // => "example.com"
// parser.port; // => "3000"
// parser.pathname; // => "/pathname/"
// parser.search; // => "?search=test"
// parser.hash; // => "#hash"
// parser.host; // => "example.com:3000"
parser.queryArray = {};
if(parser.search != undefined){
var query = parser.search.split('?');
if (query[1]) {
query = query[1].split('&');
var params = {};
for (var i = 0; i < query.length; i++){
hash = query[i].split('=');
params[hash[0]] = hash[1] === undefined ? '' : hash[1];
}
parser.queryArray = params;
}
}
return parser;
}
/**
* Add a query parameter to a URL.
*
* @param url
* The url, if not defined will be current one.
* @param key
* The parameter name.
* @param value
* The parameter value.
* @returns {string}
* The new URL or the some if the parameter already exist.
*/
function addParameterToQuery(url, key, value) {
// If not url defined, use the current one.
if (!url) {
url = document.URL;
}
var q = getQueryParameters(url);
if (q.queryArray[key] == undefined) {
q.queryArray[key] = value;
// http://example.com:3000/pathname/?search=test#hash
url = q.protocol + '//'; // => "http:"
url += q.host; // => "example.com:3000"
// parser.hostname; // => "example.com"
// parser.port; // => "3000"
url += q.pathname; // => "/pathname/"
// parser.search; // => "?search=test"
url += '?' + $.param(q.queryArray);
url += q.hash; // => "#hash"
}
return url;
}
/**
* Is the URL external to this site?
*
* @param url
* The URL.
*
* @returns {boolean}
* TRUE if is a external url.
*/
function isExternalUrl(url) {
var parser = document.createElement('a');
parser.href = url;
var root = location.protocol + '//' + location.host;
return !(parser.href.includes(root));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment