Retrieve Parameter from location bar

Discussion in 'Javascript' started by bobdydd, May 6, 2006.

  1. bobdydd

    bobdydd Guest

    Hi Everybody

    I have an Access database that passes a Parameter to a web page
    something like this.
    http://localhost/index.htm?paramName=1234-54321

    I want to be able to retrieve the parameter (In this example
    1234-54321) and pass it to a text box on a form. Something like this.

    <form name="form1" id="form1" method="post" action="">
    <input type="text" name="txtinput" value= "Param" />
    </form>

    Any help gratefully received
     
    bobdydd, May 6, 2006
    #1
    1. Advertising

  2. bobdydd

    Marcello Guest

    Marcello, May 6, 2006
    #2
    1. Advertising

  3. bobdydd

    bobdydd Guest

    Hi Marcello

    I am not too familiar with javascscipt yet but I have found this coding
    that almost does what I want but I am not fluent enough with javascript
    to achieve the effect I want.........which is to have a text box on the
    form which receives the # 1234-54321 from
    the http://localhost/index.htm?paramName=1234-54321...Url

    Thanks for the prompt reply

    Bob

    <html>
    <head>
    <title>Test</title>
    <script>
    function parseGetVars() {
    var getVars = new Array();
    var qString = unescape(top.location.search.substring(1));
    var pairs = qString.split(/\&/);
    for (var i in pairs) {
    var nameVal = pairs.split(/\=/);
    getVars[nameVal[0]] = nameVal[1];
    }
    return getVars;
    }
    </script>
    </head>

    <body>
    <script>
    var g = parseGetVars();
    for (var i in g)
    document.writeln(i+'='+g+'<br>');
    </script>

    </body>
    </html>
     
    bobdydd, May 6, 2006
    #3
  4. bobdydd

    Danny Guest

    Danny, May 7, 2006
    #4
  5. bobdydd

    ASM Guest

    bobdydd a écrit :
    > Hi Everybody
    >
    > I have an Access database that passes a Parameter to a web page
    > something like this.
    > http://localhost/index.htm?paramName=1234-54321
    >
    > I want to be able to retrieve the parameter (In this example
    > 1234-54321) and pass it to a text box on a form. Something like this.


    <head>
    <script type="text/javascript">
    if(self.location.search.length > 1)
    var myParam = self.location.search.split('=')[1];
    onload = function() {
    document.forms['form1'].elements['txtinput'].value = myParam;
    }
    </script>
    </head>
    <body>
    > <form name="form1" id="form1" method="post" action="">
    > <input type="text" name="txtinput" value= "Param" />
    > </form>


    With several parameters

    <script type="text/javascript">

    var txtinput, txtinput2;

    if(self.location.search && self.location.search.length > 1)
    {
    var myParams = self.location.search.split('?')[1]+'';
    if(myParams.indexOf('&')>=0) {
    myParams = myParams.split('&')
    for(var i=0;i<myParams.length;i++)
    {
    myParams = myParams.split('=');
    eval(myParams[0]+"='"+myParams[1]+"'");
    }
    }
    else
    {
    myParams = myParams.split('=');
    eval(myParams[0]+" = '"+myParams[1]+"'");
    }
    window.onload = function() {
    var f = document.forms['form1'].elements;
    for(var i=0;i<f.length;i++) {
    if(f.name && f.type=='text')
    {
    var oName='';
    if(eval(f.name))
    {
    eval('oName='+f.name);
    f.value = oName;
    }
    }
    }
    }
    }
    </script>

    <form name="form1" id="form1" method="post" action="">
    <input type="text" name="txtinput" value= "Param" />
    <input type="text" name="txtinput2" value= "Param" />
    </form>


    --
    Stephane Moriaux et son [moins] vieux Mac
     
    ASM, May 7, 2006
    #5
  6. bobdydd

    RobG Guest

    bobdydd wrote:
    > Hi Marcello
    >
    > I am not too familiar with javascscipt yet but I have found this coding
    > that almost does what I want but I am not fluent enough with javascript
    > to achieve the effect I want.........which is to have a text box on the
    > form which receives the # 1234-54321 from
    > the http://localhost/index.htm?paramName=1234-54321...Url
    >
    > Thanks for the prompt reply
    >
    > Bob
    >
    > <html>
    > <head>
    > <title>Test</title>
    > <script>


    The type attribute is required:

    <script type="text/javascript">

    > function parseGetVars() {


    I'd rather call it 'getSearchParams'.

    > var getVars = new Array();


    This function uses an array like an ordinary object, so it may as well
    use one (unless you want to use its array-ness for something else):

    var getVars = {};


    > var qString = unescape(top.location.search.substring(1));
    > var pairs = qString.split(/\&/);
    > for (var i in pairs) {
    > var nameVal = pairs.split(/\=/);
    > getVars[nameVal[0]] = nameVal[1];
    > }
    > return getVars;
    > }


    To get a particular parameter value, you can do:

    var searchParams = getSearchParams();
    var paramValue;
    if ( (paramValue = searchParams[paramName]) ){
    // do something with paramValue
    } else {
    // paramName was not in the searchstring
    // deal with it...
    }

    [...]

    --
    Rob
     
    RobG, May 7, 2006
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Matt
    Replies:
    0
    Views:
    3,599
  2. John Hann
    Replies:
    0
    Views:
    6,165
    John Hann
    Aug 21, 2004
  3. Matt
    Replies:
    9
    Views:
    71,235
  4. Jawahar Rajan
    Replies:
    5
    Views:
    196
  5. Matt
    Replies:
    9
    Views:
    627
    kaeli
    Aug 23, 2004
Loading...

Share This Page