File tree Expand file tree Collapse file tree 2 files changed +49
-0
lines changed
Expand file tree Collapse file tree 2 files changed +49
-0
lines changed Original file line number Diff line number Diff line change 1+ # URL Redirection Service
2+
3+ This is a simple web page that redirects to the URL encoded in the URI fragment (the part after the "#").
4+ For example < https://tom93.github.io/redirect/#https://example.com > redirects to < https://example.com > .
5+ It is useful for preventing "intelligent" software from processing URLs of a particular form.
6+
7+ The target URL can be encoded using ` encodeURIComponent() ` .
8+ Alternatively, the following function preserves most special characters for readability while still producing a valid encoding:
9+
10+ ``` js
11+ function encodeURIFragment (s ) {
12+ /* Encode a string for inclusion in the URL fragment. */
13+ /*
14+ Relevant syntax specification (https://www.rfc-editor.org/rfc/rfc3986.html):
15+ fragment = *( pchar / "/" / "?" )
16+ pchar = unreserved / pct-encoded / sub-delims / ":" / "@"
17+ unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~"
18+ sub-delims = "!" / "$" / "&" / "'" / "(" / ")"
19+ / "*" / "+" / "," / ";" / "="
20+
21+ The set of safe characters is almost the same as for encodeURI(), except encodeURI() does not encode "#".
22+ So we just use encodeURI() and then encode "#" manually.
23+ */
24+ return encodeURI (s).replace (/ #/ g , " %23" );
25+ }
26+ ```
Original file line number Diff line number Diff line change 1+ <!DOCTYPE html>
2+ < html lang ="en ">
3+ < head >
4+ < meta charset ="utf-8 ">
5+ < meta name ="viewport " content ="width=device-width,initial-scale=1 ">
6+ < link rel ="icon " href ="data:, ">
7+ < title > Redirecting...</ title >
8+ < style > body { font-family : sans-serif; }</ style >
9+ </ head >
10+ < body >
11+ < div id ="message "> </ div >
12+ < script >
13+ var url = decodeURIComponent ( location . hash . substring ( 1 ) ) ;
14+ if ( ! url ) {
15+ document . getElementById ( "message" ) . textContent = "Error: No URL specified" ;
16+ } else if ( ! / ^ h t t p [ s ] ? : \/ \/ / . test ( url ) ) {
17+ document . getElementById ( "message" ) . textContent = "Error: Invalid URL" ;
18+ } else {
19+ location . href = url ;
20+ }
21+ </ script >
22+ </ body >
23+ </ html >
You can’t perform that action at this time.
0 commit comments