www.webdeveloper.com
Results 1 to 4 of 4

Thread: Parsing urls

  1. #1
    Join Date
    Jul 2009
    Location
    UK
    Posts
    174

    Parsing urls

    hi
    The following is from a book:
    HTML Code:
    <script language="JavaScript">
    /**
    * This function parses ampersand-separated name=value argument pairs from
    * the query string of the URL. It stores the name=value pairs in 
    * properties of an object and returns that object. Use it like this:
    *
    * var args = getArgs(); //parse args from URL
    * var q = args.q || ""; //Use argument if defined, or a default value
    * var n = args.n ? parseInt(args.n) : 10;
    *
    **/
    function getArgs() {
     var args = new Object();
     var query = location.search.substring(1); //get query string
     var pairs = query.split("&"); //Break at ampersand
      for(var i =0; i< pairs.length; i++ ) {
        var pos = pairs[i].indexOf('='); //look for 'name=value'
        if(pos == -1) continue; //if not found skip
        var argname = pairs[i].substring(0,pos); //extract the name
        var value = pairs[i].substring(pos+1); // extract the value
        value = decodeURIComponent(value); //decode it, if needed
        args[argname] = value; //store as a property
       }
     return args;
    }
    </script>
    I understand how the function works! Problem is, how do i call it? I tried the following:

    PHP Code:
    <?php
    $name 
    = @$_GET['name'];
    $age = @$_GET['age'];
    ?>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xml:lang="en-GB" xmlns="http://www.w3.org/1999/xhtml" lang="en-GB">
    <head>
    <script language="JavaScript">
    /**
    * This function parses ampersand-separated name=value argument pairs from
    * the query string of the URL. It stores the name=value pairs in 
    * properties of an object and returns that object. Use it like this:
    *
    * var args = getArgs(); //parse args from URL
    * var q = args.q || ""; //Use argument if defined, or a default value
    * var n = args.n ? parseInt(args.n) : 10;
    *
    **/
    function getArgs() {
     var args = new Object();
     var query = location.search.substring(1); //get query string
     var pairs = query.split("&"); //Break at ampersand
      for(var i =0; i< pairs.length; i++ ) {
        var pos = pairs[i].indexOf('='); //look for 'name=value'
        if(pos == -1) continue; //if not found skip
        var argname = pairs[i].substring(0,pos); //extract the name
        var value = pairs[i].substring(pos+1); // extract the value
        value = decodeURIComponent(value); //decode it, if needed
        args[argname] = value; //store as a property
       }
     return args;
    }
    </script>
    </head>
    <body>

    <form action="<?php $_SERVER['PHP_SELF'?>" method="GET">
    Name: <input type="text" name="name" /><br />
    Age: <input type="text" name="age" /><br />

    <input type="Submit" value="Enter" /><br />

    </form>

    <script language="JavaScript">
    var args = getArgs(); //parse args from URL
    var q = args.q || ""; //Use argument if defined, or a default value
    var n = args.n ? parseInt(args.n, 10);
    document.write(args);

    </script>


    </body>
    </html>
    so if I enter Andrew followed by 34 into the form my url will look like:
    Code:
    "http://localhost/JavaScript/parsingURLs.php?name=Andrew&age=34"
    How do i call the function to return just the
    Code:
    name=Andrew&age=34
    part? Ive tried:

    Code:
    document.write(args);
    document.write(q);
    document.write(n);
    but no joy.

    help please..

  2. #2
    Join Date
    Nov 2009
    Posts
    11
    the javascript function you were using returns NVP
    document.write(location.search.substring(1)) will return the string you wanted, no need for the function here

  3. #3
    Join Date
    Oct 2008
    Location
    U.S.
    Posts
    726
    If you want to use the function to retrieve the individual values, the following is how you would go about it:
    Code:
    <script language="JavaScript"> 
    var args = getArgs(); //parse args from URL 
    var q = args.age || "no age variable"; //Use argument if defined, or a default value 
    var n = args.name || "no name variable"; 
    document.write(q +"<br>"+ n); 
    </script>
    Note how the 'age' and 'name' properties correlate to what is expected as the form inputs are named 'age' and 'name', so you query getArgs for properties with those same names.
    Last edited by astupidname; 11-26-2009 at 07:46 PM.

  4. #4
    Join Date
    Jul 2009
    Location
    UK
    Posts
    174
    Quote Originally Posted by astupidname View Post
    Note how the 'age' and 'name' properties correlate to what is expected as the form inputs are named 'age' and 'name', so you query getArgs for properties with those same names.
    That makes more sense, thanks. You'd expect a book to be correct wouldn't you? Why do so many programming books have errors? They lead one down a totally incorrect path!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles