Skip to content

Commit cddac96

Browse files
committed
adding examples for fragment abuse with URI.js (Issue #2)
1 parent 1a691e9 commit cddac96

File tree

2 files changed

+122
-0
lines changed

2 files changed

+122
-0
lines changed

src/URI.fragmentQuery.js

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* Extending URL.js for fragment abuse
3+
*/
4+
5+
// --------------------------------------------------------------------------------
6+
// EXAMPLE: storing application/x-www-form-urlencoded data in the fragment
7+
// possibly helpful for Google's hashbangs
8+
// see http://code.google.com/web/ajaxcrawling/
9+
// --------------------------------------------------------------------------------
10+
11+
// USAGE:
12+
// var uri = URI("http://example.org/#?foo=bar");
13+
// uri.fragment(true) === {foo: "bar"};
14+
// uri.fragment({bar: "foo"});
15+
// uri.toString() === "http://example.org/#?bar=foo";
16+
// uri.addFragment("name", "value");
17+
// uri.toString() === "http://example.org/#?bar=foo&name=value";
18+
// uri.removeFragment("name");
19+
// uri.toString() === "http://example.org/#?bar=foo";
20+
21+
(function(URI, undefined){
22+
23+
var p = URI.prototype,
24+
// old fragment handler we need to wrap
25+
f = p.fragment,
26+
// NOTE: google want's #! (hashbang), others might want #? others might want plain #
27+
// choose the prefix you want to use here
28+
prefix = '?';
29+
30+
// add fragment(true) and fragment({key: value}) signatures
31+
p.fragment = function(v, build) {
32+
if (v === true) {
33+
return URI.parseQuery(this._parts.fragment.substring(prefix.length));
34+
} else if (v !== undefined && typeof v !== "string") {
35+
this._parts.fragment = prefix + URI.buildQuery(v);
36+
this.build(!build);
37+
return this;
38+
} else {
39+
return f.call(this, v, build);
40+
}
41+
};
42+
p.addFragment = function(name, value, build) {
43+
var data = URI.parseQuery(this._parts.fragment.substring(prefix.length));
44+
URI.addQuery(data, name, value);
45+
this._parts.fragment = prefix + URI.buildQuery(data);
46+
if (typeof name !== "string") {
47+
build = value;
48+
}
49+
50+
this.build(!build);
51+
return this;
52+
};
53+
p.removeFragment = function(name, value, build) {
54+
var data = URI.parseQuery(this._parts.fragment.substring(prefix.length));
55+
URI.removeQuery(data, name, value);
56+
this._parts.fragment = prefix + URI.buildQuery(data);
57+
if (typeof name !== "string") {
58+
build = value;
59+
}
60+
61+
this.build(!build);
62+
return this;
63+
};
64+
p.addHash = p.addFragment;
65+
p.removeHash = p.removeFragment;
66+
67+
})(window.URI);

src/URI.fragmentURI.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Extending URL.js for fragment abuse
3+
*/
4+
5+
// --------------------------------------------------------------------------------
6+
// EXAMPLE: storing a relative URL in the fragment ("FragmentURI")
7+
// possibly helpful when working with backbone.js or sammy.js
8+
// inspired by https://github.com/medialize/URI.js/pull/2
9+
// --------------------------------------------------------------------------------
10+
11+
// USAGE:
12+
// var uri = URI("http://example.org/#!/foo/bar/baz.html"),
13+
// furi = uri.fragment(true);
14+
// furi.pathname() === '/foo/bar/baz.html';
15+
// furi.pathname('/hello.html');
16+
// uri.toString() === "http://example.org/#!/hello.html"
17+
18+
(function(URI, undefined){
19+
20+
var p = URI.prototype,
21+
// old handlers we need to wrap
22+
f = p.fragment,
23+
b = p.build,
24+
// NOTE: google want's #! (hashbang), others might want plain #
25+
// choose the prefix you want to use here
26+
prefix = '!';
27+
28+
// add fragment(true) and fragment(URI) signatures
29+
p.fragment = function(v, build) {
30+
if (v === true) {
31+
var furi = new URI(this._parts.fragment.substring(prefix.length));
32+
this._furi = furi;
33+
return furi;
34+
} else if (v !== undefined && typeof v !== "string") {
35+
this._furi = furi;
36+
this._parts.fragment = prefix + v.toString();
37+
this.build(!build);
38+
return this;
39+
} else if (typeof v === "string") {
40+
this._furi = undefined;
41+
}
42+
43+
return f.call(this, v, build);
44+
};
45+
46+
// make .build() of the actual URI aware of the FragmentURI
47+
p.build = function(deferBuild) {
48+
if (this._furi) {
49+
this._parts.fragment = prefix + this._furi.toString();
50+
}
51+
52+
return b.call(this, deferBuild);
53+
};
54+
55+
})(window.URI);

0 commit comments

Comments
 (0)