Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

extreme n00b question

Options
  • 23-06-2006 12:10pm
    #1
    Registered Users Posts: 4,222 ✭✭✭


    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 Posts: 68,317 ✭✭✭✭seamus


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


  • Registered Users Posts: 4,222 ✭✭✭Scruff


    sorry, yeah using javascript.


  • Registered Users 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