SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Jul 2011
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Need help with parsing a URL list to get the host names

    Hello All,

    I have a form where I ask my users for a comma seperated web site list.
    I want to make sure this list only contains the host names of URLs without any white spaces

    Original list: site1.com, http://site2.com, http://www.site3.com,sub.site4.com
    Edited list: site1.com,site2.com,site3.com,sub.site4.com

    Currently I have the following to get the comma separated list

    Code:
    var list = document.getElementById( "list" ).value ;
    Can some one please help me with above task of getting host names and trimming for white spaces?
    I am very new to JavaScript language and highly appreciate your help or direction.

  2. #2
    SitePoint Enthusiast
    Join Date
    Jun 2011
    Location
    Dartmouth, MA, USA
    Posts
    49
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can use the following code, which will remove all whitespace from the list:

    Code JavaScript:
    var list = document.getElementById("list").value.replace(/\s/g, "");

    The replace() function is a String function that takes a regular expression to search for, and then a replacement string. The regular expression /\s/ looks for any kind of whitespace, and the g modifier on the end instructs the replace function to make the replacement throughout the entire string, and not to just stop after the first match. The second argument to the function, the empty string ("") tells it to replace all whitespace it finds with nothing, effectively removing all whitespace from the string.

  3. #3
    SitePoint Member
    Join Date
    Jul 2011
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have found some sample codes at

    It explains how to get the host from a given single URL and I can use bhawk90's for white space.

    However, I am still not sure how to do the host name conversion for the whole list. Need to do some iteration but no knowledge regarding that.

    Code JavaScript:
    // This function creates a new anchor element and uses location
    // properties (inherent) to get the desired URL data. Some String
    // operations are used (to normalize results across browsers).
     
    function parseURL(url) {
        var a =  document.createElement('a');
        a.href = url;
        return {
            source: url,
            protocol: a.protocol.replace(':',''),
            host: a.hostname,
            port: a.port,
            query: a.search,
            params: (function(){
                var ret = {},
                    seg = a.search.replace(/^\?/,'').split('&'),
                    len = seg.length, i = 0, s;
                for (;i<len;i++) {
                    if (!seg[i]) { continue; }
                    s = seg[i].split('=');
                    ret[s[0]] = s[1];
                }
                return ret;
            })(),
            file: (a.pathname.match(/\/([^\/?#]+)$/i) || [,''])[1],
            hash: a.hash.replace('#',''),
            path: a.pathname.replace(/^([^\/])/,'/$1'),
            relative: (a.href.match(/tps?:\/\/[^\/]+(.+)/) || [,''])[1],
            segments: a.pathname.replace(/^\//,'').split('/')
        };
    }

    This need to be iterated (I think)
    Code:
    var myURL = parseURL(single_URL Here)

    Please help if you know how to do this. Greatly appreciate it.

  4. #4
    SitePoint Enthusiast
    Join Date
    Jun 2011
    Location
    Dartmouth, MA, USA
    Posts
    49
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Something like this should do it:

    Code JavaScript:
    var list = document.getElementById("list").value.replace(/\s/g, "");
    var urls = list.split(",");
    for (i in urls) {
    parsed_url = parseURL(urls[i]);
    // Do something with parsed_url
    }

    After removing all whitespace, the variable urls is set to an array of the submitted urls, which is created by telling the split function which character should be used as a separator, which in this case is a comma. Then, using a for...in loop, you loop through all the urls in the urls array and call the parseURL function on them, and do what you need to do with the parse_url variable.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •