Skip to content

Instantly share code, notes, and snippets.

@alash3al
Last active August 29, 2015 14:15
Show Gist options
  • Save alash3al/3993f573711e1193211b to your computer and use it in GitHub Desktop.
Save alash3al/3993f573711e1193211b to your computer and use it in GitHub Desktop.
Parse a url and return its components, php parse_url() for javascript
/**
* Parse a url and return its components
*
* @param string $url
* @return Object
*
* @author Mohammed Al Ashaal <is.gd/alash3al>, inspired by PHP
* @version 1.0.0
*/
function parse_url($url)
{
$dom = document.createElement('a');
$dom.href = $url;
$obj =
{
scheme : $dom.protocol.substr(0, $dom.protocol.indexOf(':')),
host : $dom.hostname,
port : $dom.port,
path : $dom.pathname,
query : $dom.search.substr($dom.search.indexOf('?') + 1),
hash : $dom.hash.substr($dom.hash.indexOf('#'))
}
return $obj;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment