forked from DefinitelyTyped/DefinitelyTyped
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Alexey Gorshkov
committed
May 10, 2015
1 parent
b3834d8
commit 88b29b7
Showing
2 changed files
with
85 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/// <reference path="jsurl.d.ts" /> | ||
|
||
var u = new Url; // curent document URL will be used | ||
// or we can instantiate as | ||
var u2 = new Url("http://example.com/some/path?a=b&c=d#someAnchor"); | ||
// it should support relative URLs also | ||
var u3 = new Url("/my/site/doc/path?foo=bar#baz"); | ||
|
||
// get the value of some query string parameter | ||
alert(u2.query.a); | ||
// or | ||
alert(u3.query["foo"]); | ||
|
||
// Manupulating query string parameters | ||
u.query.a = [1, 2, 3]; // adds/replaces in query string params a=1&a=2&a=3 | ||
u.query.b = 'woohoo'; // adds/replaces in query string param b=woohoo | ||
|
||
if (u.query.a instanceof Array) { // the way to add a parameter | ||
u.query.a.push(4); // now it's "a=1&a=2&a=3&a=4&b=woohoo" | ||
} | ||
|
||
else { // if not an array but scalar value here is a way how to convert to array | ||
u.query.a = [u.query.a]; | ||
u.query.a.push(8) | ||
} | ||
|
||
|
||
// The way to remove the parameter: | ||
delete u.query.a | ||
// or: | ||
delete u.query["a"] | ||
|
||
// If you need to remove all query string params: | ||
u.query.clear(); | ||
alert(u); | ||
|
||
// Lookup URL parts: | ||
alert( | ||
'protocol = ' + u.protocol + '\n' + | ||
'user = ' + u.user + '\n' + | ||
'pass = ' + u.pass + '\n' + | ||
'host = ' + u.host + '\n' + | ||
'port = ' + u.port + '\n' + | ||
'path = ' + u.path + '\n' + | ||
'query = ' + u.query + '\n' + | ||
'hash = ' + u.hash | ||
); | ||
|
||
// Manipulating URL parts | ||
u.path = '/some/new/path'; // the way to change URL path | ||
u.protocol = 'https' // the way to force https protocol on the source URL | ||
|
||
// inject into string | ||
var str = '<a href="' + u + '">My Cool Link</a>'; | ||
|
||
// or use in DOM context | ||
var a = document.createElement('a'); | ||
a.href = u; | ||
a.innerHTML = 'test'; | ||
document.body.appendChild(a); | ||
|
||
// Stringify | ||
u += ''; | ||
String(u); | ||
u.toString(); | ||
// NOTE, that usually it will be done automatically, so only in special | ||
// cases direct stringify is required |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// Type definitions for jsurl 1.2.2 | ||
// Project: https://github.com/Mikhus/jsurl | ||
// Definitions by: Alexey Gorshkov <https://github.com/agorshkov23> | ||
// Definitions: https://github.com/agorshkov23/DefinitelyTyped | ||
|
||
declare class Url { | ||
constructor(url?: string); | ||
query: any; | ||
protocol: string; | ||
user: string; | ||
pass: string; | ||
host: string; | ||
port: string; | ||
path: string; | ||
hash: string; | ||
href: string; | ||
toString(): string; | ||
} |