kurl
Version:
It's a powerfull URL parser written in JavaScript and compatible with NodeJS with a lot of features that it will help you to modify/parse easily a URL without any problem.
98 lines (53 loc) • 1.4 kB
HTML
<html>
<head>
<title>JavaScript/NodeJS URL</title>
<script type="text/javascript" src="../url.js"></script>
<script type="text/javascript">
var url = URL('http://guest:secret@remote:21/filename?a=1#ok');
console.log(
url.attr()
/*{
protocol: 'http:',
auth: 'guest:secret',
host: 'remote:21',
hostname: 'remote',
port: 21,
pathname: '/filename',
search: '?a=1',
hash: '#ok'
}*/
);
console.log(
url.attr('hostname')
// remote
);
console.log(
url.attr('hostname', 'localhost').href()
// http://guest:secret@localhost:21/filename?a=1#ok
);
console.log(
url.attr('search'),
// ?a=1
url.query('b', 2).query(),
// { a : 1, b : 2 }
url.attr('search')
// ?a=1&b=2
);
console.log(
url.hash({ a: 1 }).select('hostname', 'pathname', 'hash')
// //localhost/filename#a=1
);
console.log(
URL('http://localhost', 'https://localhost').isExternal(),
// true
URL('/folder', 'https://localhost').isExternal()
// false
);
console.log(
URL('subsection?a=1', 'http://localhost/section/').from('pathname')
// /section/subsection?a=1
);
</script>
</head>
<body>See the console log.</body>
</html>