Skip to content

Instantly share code, notes, and snippets.

@pankajparashar-zz
Created July 6, 2013 07:25
Show Gist options
  • Save pankajparashar-zz/5939101 to your computer and use it in GitHub Desktop.
Save pankajparashar-zz/5939101 to your computer and use it in GitHub Desktop.
Parse <url> with Javascript.
$(function(){
// The URL we want to parse
var url = 'http://tutorialzine.com/2013/07/quick-tip-parse-urls/?key=value#comments';
// The magic: create a new anchor element, and set the URL as its href attribute.
// Notice that I am accessing the DOM element inside the jQuery object with [0]:
var a = $('<a>', { href:url } )[0];
$('#host').val(a.hostname);
$('#path').val(a.pathname);
$('#query').val(a.search);
$('#hash').val(a.hash);
// Even more:
// a.port, a.protocol,
// a.origin (not available in older IE versions)
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment