/** [1] Example of use of URL class */
// Creating a new object
var u = new URL("http://user%20test:password%20test@example-test_hi.com:80/test/index.php?a=1&b=2&c[]=x%20y&d&#foo");
// Modifying some URL components
u.addParam("test1", "abc").addParam("test2", "123").addParam("c[10]", "klm").removeParam("a").set("fragment", "bar").set("port", 8080);
// Rebuild URL
var str_url = u.buildURL();
// Check if there is a param at query
var has_c = u.hasParam("c");
// Showing the new URL and its components
window.alert(
"Example 1\n" +
"New URL: " + str_url + "\n" +
"Scheme: " + u.scheme + "\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" +
"Fragment: " + u.fragment
);
/** [2] Example of simple inline URL manipulation */
var str_url = (new URL("http://test.com/index.php?a=1")).clearParams().addParam("a", "2").buildURL();
window.alert("Example 2: " + str_url);
/** [3] Same example with many lines */
var u = new URL();
u.parseURL("http://test.com/index.php?a=1");
u.clearParams();
u.addParam("a", "2");
var str_url = u.buildURL();
window.alert("Example 3: " + str_url); |