Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

extreme n00b question

  • 23-06-2006 12:10PM
    #1
    Registered Users, Registered Users 2 Posts: 4,228 ✭✭✭


    I'm emabarrassed to ask this but i'm drawing a complete blank :o
    How do you read the parameters from a http req? e.g from
    http://www.foo.com/home.html?param1=test&param3=123
    

    want to use the parameters in an onload function to make decisions on what to display.


Comments

  • Registered Users, Registered Users 2 Posts: 68,173 ✭✭✭✭seamus


    What language are you using? Or are you trying to do it through Javascript?


  • Registered Users, Registered Users 2 Posts: 4,228 ✭✭✭Scruff


    sorry, yeah using javascript.


  • Registered Users, Registered Users 2 Posts: 3,594 ✭✭✭forbairt


    like gandalf said ... YOU SHALL NOT PARSE ...

    oh wait ... in this case I reckon you need to parse the url ...
    <script type="text/Javascript">
    function PageQuery(q) {
    if(q.length > 1) this.q = q.substring(1, q.length);
    else this.q = null;
    this.keyValuePairs = new Array();
    if(q) {
    for(var i=0; i < this.q.split("&").length; i++) {
    this.keyValuePairs[i] = this.q.split("&")[i];
    }
    }
    this.getKeyValuePairs = function() { return this.keyValuePairs; }
    this.getValue = function(s) {
    for(var j=0; j < this.keyValuePairs.length; j++) {
    if(this.keyValuePairs[j].split("=")[0] == s)
    return this.keyValuePairs[j].split("=")[1];
    }
    return false;
    }
    this.getParameters = function() {
    var a = new Array(this.getLength());
    for(var j=0; j < this.keyValuePairs.length; j++) {
    a[j] = this.keyValuePairs[j].split("=")[0];
    }
    return a;
    }
    this.getLength = function() { return this.keyValuePairs.length; }
    }
    
    function queryString(key){
    var page = new PageQuery(window.location.search);
    return unescape(page.getValue(key));
    }
    
    function displayItem(key){
    if(queryString(key)=='false')
    {
    alert("you didn't enter a ?name=value querystring item.");
    }else{
    alert(queryString(key));
    }
    }
    </script>
    <body onload="displayItem('name');">
    <form action="test.php" method="get">
    
    <input name="name" type="text" />
    <input type="submit" />
    </form>
    


Advertisement