Last active
August 29, 2015 14:15
-
-
Save alash3al/3993f573711e1193211b to your computer and use it in GitHub Desktop.
Parse a url and return its components, php parse_url() for javascript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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