Login   Register  
Icontem

File: test.js

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Rubens Takiguti Ribeiro  >  URL  >  test.js  >  Download  
File: test.js
Role: Example script
Content type: text/plain
Description: Example of use.
Class: URL
Parse and manipulate URLs and query arguments
Author: By
Last change: wrong file.
Date: 2011-05-24 22:05
Size: 1,282 bytes
 

Contents

Class file image Download
/** [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);