Skip to content

Commit

Permalink
Added arguments for sub,file,filename,fileext
Browse files Browse the repository at this point in the history
  • Loading branch information
Nova committed Oct 27, 2012
1 parent d241426 commit d6160ff
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 20 deletions.
16 changes: 11 additions & 5 deletions README.md
Expand Up @@ -6,17 +6,23 @@ A jQuery $.uri() method for quickly parsing a uri.
## Examples

```html
http://www.domain.com/path/name?query1=test&silly=willy#test=hash&chucky=cheese
http://www.domain.com/path/index.html?query1=test&silly=willy#test=hash&chucky=cheese
```

```javascript
$.uri(); // http://www.domain.com/path/name?query1=test&silly=willy#test=hash&chucky=cheese
$.uri(); // http://www.domain.com/path/index.html?query1=test&silly=willy#test=hash&chucky=cheese
$.uri('domain'); // www.domain.com
$.uri('tld'); // domain.com
$.uri('path'); // /path/name/
$.uri('host'); // domain.com
$.uri('tld'); // com
$.uri('sub'); // www
$.uri('path'); // /path/index.html
$.uri('file'); // index.html
$.uri('filename'); // index
$.uri('fileext'); // html
$.uri('1'); // path
$.uri('2'); // name
$.uri('2'); // index.html
$.uri('3'); // (an empty string)
$.uri('-1'); // index.html
$.uri('?'); // query1=test&silly=willy
$.uri('?silly'); // willy
$.uri('?poo'); // (an empty string)
Expand Down
33 changes: 20 additions & 13 deletions index.html
Expand Up @@ -10,19 +10,26 @@
<script type="text/javascript">

console.log($.uri());
console.log($.uri('domain'));
console.log($.uri('tld'));
console.log($.uri('path'));
console.log($.uri('1'));
console.log($.uri('2'));
console.log($.uri('3'));
console.log($.uri('4'));
console.log($.uri('?'));
console.log($.uri('?test'));
console.log($.uri('?poo'));
console.log($.uri('#'));
console.log($.uri('#this'));
console.log($.uri('#test'));
console.log('domain: ' + $.uri('domain'));
console.log('host: ' + $.uri('host'));
console.log('tld: ' + $.uri('tld'));
console.log('sub: ' + $.uri('sub'));
console.log('path: ' + $.uri('path'));
console.log('file: ' + $.uri('file'));
console.log('filename: ' + $.uri('filename'));
console.log('fileext: ' + $.uri('fileext'));
console.log('1: ' + $.uri('1'));
console.log('2: ' + $.uri('2'));
console.log('3: ' + $.uri('3'));
console.log('-1: ' + $.uri('-1'));
console.log('-2: ' + $.uri('-2'));
console.log('-3: ' + $.uri('-3'));
console.log('?: ' + $.uri('?'));
console.log('?test: ' + $.uri('?test'));
console.log('?poo: ' + $.uri('?poo'));
console.log('#: ' + $.uri('#'));
console.log('#this: ' + $.uri('#this'));
console.log('#test: ' + $.uri('#test'));

</script>
</head>
Expand Down
21 changes: 19 additions & 2 deletions jquery-uri.js
Expand Up @@ -18,9 +18,26 @@ jQuery.extend(
{
if(!arg) return window.location.toString();
else if(arg === 'domain') return window.location.hostname;
else if(arg === 'tld') return window.location.hostname.split('.').slice(-2).join('.');
else if(arg === 'host') return window.location.hostname.split('.').slice(-2).join('.');
else if(arg === 'tld') return window.location.hostname.split('.').slice(-1).join('.');
else if(arg === 'sub')
{
var domain = window.location.hostname.split('.');

if(domain.length < 3) return '';
else return domain.slice(0, domain.length - 2).join('.');
}
else if(arg === 'path') return window.location.pathname;
else if($.isNumeric(arg)) return window.location.pathname.split('/')[arg] || '';
else if($.isNumeric(arg))
{
var path = window.location.pathname.split('/');

arg = parseInt(arg);
return path[arg < 0 ? path.length + arg : arg] || '';
}
else if(arg === 'file') return window.location.pathname.split('/').slice(-1);
else if(arg === 'filename') return window.location.pathname.split('/').slice(-1)[0].split('.')[0];
else if(arg === 'fileext') return window.location.pathname.split('/').slice(-1)[0].split('.')[1] || '';
else if(arg[0] === '?' || arg[0] === '#')
{
var params = window.location.toString(), param = null;
Expand Down

0 comments on commit d6160ff

Please sign in to comment.