Created
March 7, 2012 18:16
-
-
Save tikitikipoo/1994829 to your computer and use it in GitHub Desktop.
parse url
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
<!DOCTYPE HTML> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<script src="./parse_url.js"></script> | |
<script type="text/javascript"> | |
var url1 = "http://jblas:[email protected]:8080/mail/inbox?msg=1234&type=unread#msg-content"; | |
var parse1 = path.parseUrl(url1); | |
var url2 = "http://mycompany.com/mail/inbox?msg=1234&type=unread#msg-content"; | |
var parse2 = path.parseUrl(url2); | |
console.log(parse1); | |
console.log(parse2); | |
</script> | |
<title></title> | |
</head> | |
<body> | |
</body> | |
</html> |
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
// below code almost from jQuery Mobile | |
// I modify a little in order to use without jQuery Library | |
//url path helpers for use in relative url management | |
var path = { | |
// This scary looking regular expression parses an absolute URL or its relative | |
// variants (protocol, site, document, query, and hash), into the various | |
// components (protocol, host, path, query, fragment, etc that make up the | |
// URL as well as some other commonly used sub-parts. When used with xp.exec() | |
// or String.match, it parses the URL into a results array that looks like this: | |
// | |
// [0]: http://jblas:[email protected]:8080/mail/inbox?1234&type=unread#msg-content | |
// [1]: http://jblas:[email protected]:8080/mail/inbox?1234&type=unread | |
// [2]: http://jblas:[email protected]:8080/mail/inbox | |
// [3]: http://jblas:[email protected]:8080 | |
// [4]: http: | |
// [5]: // | |
// [6]: jblas:[email protected]:8080 | |
// [7]: jblas:password | |
// [8]: jblas | |
// [9]: password | |
// [10]: mycompany.com:8080 | |
// [11]: mycompany.com | |
// [12]: 8080 | |
// [13]: /mail/inbox | |
// [14]: /mail/ | |
// [15]: inbox | |
// [16]: ?msg=1234&type=unread | |
// [17]: #msg-content | |
// | |
urlParseRE: /^(((([^:¥/#¥?]+:)?(?:(¥/¥/)((?:(([^:@¥/#¥?]+)(?:¥:([^:@¥/#¥?]+))?)^:¥/#¥?¥]¥[]+|¥[[^¥/¥]@#?]+¥])(?:¥:([0-9]+))?))?)?)?((¥/?(?:[^¥/¥?#]+¥/+)*)([^¥??(¥?[^#]+)?)(#.*)?/, | |
//Parse a URL into a structure that allows easy access to | |
//all of the URL components by name. | |
parseUrl: function( url ) { | |
// If we're passed an object, we'll assume that it is | |
// a parsed url object and just return it back to the caller. | |
if ( typeof url === "object" ) { | |
return url; | |
} | |
var matches = path.urlParseRE.exec( url || "" ) || []; | |
// Create an object that allows the caller to access the sub-matches | |
// by name. Note that IE returns an empty string instead of undefined, | |
// like all other browsers do, so we normalize everything so its stent | |
// no matter what browser we're running on. | |
return { | |
href: matches[ 0 ] || "", | |
hrefNoHash: matches[ 1 ] || "", | |
hrefNoSearch: matches[ 2 ] || "", | |
domain: matches[ 3 ] || "", | |
protocol: matches[ 4 ] || "", | |
doubleSlash: matches[ 5 ] || "", | |
authority: matches[ 6 ] || "", | |
username: matches[ 8 ] || "", | |
password: matches[ 9 ] || "", | |
host: matches[ 10 ] || "", | |
hostname: matches[ 11 ] || "", | |
port: matches[ 12 ] || "", | |
pathname: matches[ 13 ] || "", | |
directory: matches[ 14 ] || "", | |
filename: matches[ 15 ] || "", | |
search: matches[ 16 ] || "", | |
hash: matches[ 17 ] || "" | |
}; | |
}, | |
//Turn relPath into an asbolute path. absPath is | |
//an optional absolute path which describes what | |
//relPath is relative to. | |
makePathAbsolute: function( relPath, absPath ) { | |
if ( relPath && relPath.charAt( 0 ) === "/" ) { | |
return relPath; | |
} | |
relPath = relPath || ""; | |
absPath = absPath ? absPath.replace( /^¥/|(¥/[^¥/]*|[^¥/]+)$/g, "" ) : ""; | |
var absStack = absPath ? absPath.split( "/" ) : [], | |
relStack = relPath.split( "/" ); | |
for ( var i = 0; i < relStack.length; i++ ) { | |
var d = relStack[ i ]; | |
switch ( d ) { | |
case ".": | |
break; | |
case "..": | |
if ( absStack.length ) { | |
absStack.pop(); | |
} | |
break; | |
default: | |
absStack.push( d ); | |
break; | |
} | |
} | |
return "/" + absStack.join( "/" ); | |
}, | |
//Returns true if both urls have the same domain. | |
isSameDomain: function( absUrl1, absUrl2 ) { | |
return path.parseUrl( absUrl1 ).domain === path.parseUrlUrl2 ).domain; | |
}, | |
//Returns true for any relative variant. | |
isRelativeUrl: function( url ) { | |
// All relative Url variants have one thing in common, no protocol. | |
return path.parseUrl( url ).protocol === ""; | |
}, | |
//Returns true for an absolute url. | |
isAbsoluteUrl: function( url ) { | |
return path.parseUrl( url ).protocol !== ""; | |
}, | |
//Turn the specified realtive URL into an absolute one. This function | |
//can handle all relative variants (protocol, site, document, query, fragment). | |
makeUrlAbsolute: function( relUrl, absUrl ) { | |
if ( !path.isRelativeUrl( relUrl ) ) { | |
return relUrl; | |
} | |
var relObj = path.parseUrl( relUrl ), | |
absObj = path.parseUrl( absUrl ), | |
protocol = relObj.protocol || absObj.protocol, | |
doubleSlash = relObj.protocol ? relObj.doubleSlash : bj.doubleSlash || absObj.doubleSlash ), | |
authority = relObj.authority || absObj.authority, | |
hasPath = relObj.pathname !== "", | |
pathname = path.makePathAbsolute( relObj.pathname || bj.filename, absObj.pathname ), | |
search = relObj.search || ( !hasPath && absObj.search ) || "", | |
hash = relObj.hash; | |
return protocol + doubleSlash + authority + pathname + search + hash; | |
}, | |
//Add search (aka query) params to the specified url. | |
addSearchParams: function( url, params ) { | |
var u = path.parseUrl( url ), | |
p = ( typeof params === "object" ) ? $.param( params ) : params, | |
s = u.search || "?"; | |
return u.hrefNoSearch + s + ( s.charAt( s.length - 1 ) !== "?" ? "&" : "" ) + p hash || "" ); | |
}, | |
convertUrlToDataUrl: function( absUrl ) { | |
var u = path.parseUrl( absUrl ); | |
if ( path.isEmbeddedPage( u ) ) { | |
// For embedded pages, remove the dialog hash key as in getFilePath(), | |
// otherwise the Data Url won't match the id of the embedded Page. | |
return u.hash.split( dialogHashKey )[0].replace( /^#/, "" ); | |
} else if ( path.isSameDomain( u, documentBase ) ) { | |
return u.hrefNoHash.replace( documentBase.domain, "" ); | |
} | |
return absUrl; | |
}, | |
//get path from current hash, or from a file path | |
get: function( newPath ) { | |
if( newPath === undefined ) { | |
newPath = location.hash; | |
} | |
return path.stripHash( newPath ).replace( /[^¥/]*¥.[^¥/*]+$/, '' ); | |
}, | |
//return the substring of a filepath before the sub-page key, for making a r request | |
getFilePath: function( path ) { | |
var splitkey = '&' + $.mobile.subPageUrlKey; | |
return path && path.split( splitkey )[0].split( dialogHashKey )[0]; | |
}, | |
//set location hash to path | |
set: function( path ) { | |
location.hash = path; | |
}, | |
//test if a given url (string) is a path | |
//NOTE might be exceptionally naive | |
isPath: function( url ) { | |
return ( /¥// ).test( url ); | |
}, | |
//return a url path with the window's location protocol/hostname/pathname ved | |
clean: function( url ) { | |
return url.replace( documentBase.domain, "" ); | |
}, | |
//just return the url without an initial # | |
stripHash: function( url ) { | |
return url.replace( /^#/, "" ); | |
}, | |
//remove the preceding hash, any query params, and dialog notations | |
cleanHash: function( hash ) { | |
return path.stripHash( hash.replace( /¥?.*$/, "" ).replace( dialogHashKey, | |
}, | |
//check whether a url is referencing the same domain, or an external domain ferent protocol | |
//could be mailto, etc | |
isExternal: function( url ) { | |
var u = path.parseUrl( url ); | |
return u.protocol && u.domain !== documentUrl.domain ? true : false; | |
}, | |
hasProtocol: function( url ) { | |
return ( /^(:?¥w+:)/ ).test( url ); | |
}, | |
//check if the specified url refers to the first page in the main application ment. | |
isFirstPageUrl: function( url ) { | |
// We only deal with absolute paths. | |
var u = path.parseUrl( path.makeUrlAbsolute( url, documentBase ) ), | |
// Does the url have the same path as the document? | |
samePath = u.hrefNoHash === documentUrl.hrefNoHash || umentBaseDiffers && u.hrefNoHash === documentBase.hrefNoHash ), | |
// Get the first page element. | |
fp = $.mobile.firstPage, | |
// Get the id of the first page element if it has one. | |
fpId = fp && fp[0] ? fp[0].id : undefined; | |
// The url refers to the first page if the path matches the document and | |
// it either has no hash value, or the hash is exactly equal to the id of | |
// first page element. | |
return samePath && ( !u.hash || u.hash === "#" || ( fpId && h.replace( /^#/, "" ) === fpId ) ); | |
}, | |
isEmbeddedPage: function( url ) { | |
var u = path.parseUrl( url ); | |
//if the path is absolute, then we need to compare the url against | |
//both the documentUrl and the documentBase. The main reason for this | |
//is that links embedded within external documents will refer to the | |
//application document, whereas links embedded within the application | |
//document will be resolved against the document base. | |
if ( u.protocol !== "" ) { | |
return ( u.hash && ( u.hrefNoHash === documentUrl.hrefNoHash || umentBaseDiffers && u.hrefNoHash === documentBase.hrefNoHash ) ) ); | |
} | |
return (/^#/).test( u.href ); | |
} | |
}, | |
//existing base tag? | |
$base = document.getElementsByTagName("base")[0], | |
//tuck away the original document URL minus any fragment. | |
documentUrl = path.parseUrl( location.href ), | |
//if the document has an embedded base tag, documentBase is set to its | |
//initial value. If a base tag does not exist, then we default to the documentUrl. | |
documentBase = $base && $base.length ? path.parseUrl( path.makeUrlAbsolutese.attr( "href" ), documentUrl.href ) ) : documentUrl, | |
//cache the comparison once. | |
documentBaseDiffers = ( documentUrl.hrefNoHash !== documentBase.hrefNoHash ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment