Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Mar 2012
    Posts
    15
    Thanks
    0
    Thanked 1 Time in 1 Post

    Accessing the query string.

    I have a some javascript that imbeds a survey into a webpage I created. In the script is a field which allows you to define pre-filled values:

    'defaultValues':'field816=resIDnum',

    The customer wants to give the person filling out the form a URL that has "field816=resIDnum" in it. The only problem is the survey program does not allow me to use URL appends. I need to use the javascipt.

    I want to make a script that automatically fills the 'defaultValues' field in the JavaScript.

    I was thinking about telling the customer to make there URL's in this format:

    www.example.com/whw/form?12345

    Then I was thinking about defining a variable of "rval" then using window.location.search to get the numbers (12345). And once I have that input "rval" into the 'defaultValues' field.

    Do you think that this is the most efficient way? Or do you have a better way?

    (PS. If you can't understand what I'm trying to say then please ask. I'll get back instantly)

    Thanks -Chris

  • #2
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,396
    Thanks
    12
    Thanked 569 Times in 562 Posts
    Yes, ok, your description is perfectly clear. So what is your problem? If you specify it like this
    Code:
    var rval = window.location.search.substring(1);
    (as you just proposed), it should already work. But I don't know what you mean by "defaultValues" field in Javascript. Is it a HTML form field? Is it a Javascript variable?

  • #3
    New Coder
    Join Date
    Mar 2012
    Posts
    15
    Thanks
    0
    Thanked 1 Time in 1 Post
    I'm embedding the survey into a website I created, and there are some values which control the functions of the survey.

    Code:
    var (removed for security);(function(d, t) {
    var s = d.createElement(t), options = {
    'userName':'(removed for security)', 
    'formHash':'(removed for security)', 
    'autoResize':true,
    
    // This is where we add the guest ID number.
    'defaultValues':'field816=',
    
    'height':'3521',
    'async':true,
    'header':'show'};
    I created a script to get the numbers after the ? in the domain:

    Code:
    var querystring = window.location.search;
    
    // Strip the unwanted values 
    var querystringValue = querystring.substring(querystring.indexOf("=")+1, querystring.length);
    
    // Treat it as a numeric value
    var number = querystringValue-0;
    And I need to input the variable "querystring" into the defaultValues field. But it keeps coming up as the text "querystring" instead of the variable numbers. I'm writing the code in dreamweaver, and whenever I try to specify the variable it gives me a error.

    Hope this clears things up -Chris

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,210
    Thanks
    75
    Thanked 4,343 Times in 4,309 Posts
    HINT: Never never never trust DreamWeaver (better known as NightmareWaker or maybe DrunkWhacker) when it comes to JavaScript.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,210
    Thanks
    75
    Thanked 4,343 Times in 4,309 Posts
    Anyway, you originally said you were going to use something like
    Code:
         www.example.com/whw/form?12345
    But now you have your code looking for an = character in the querystring, as if you had
    Code:
        www.example.com/whw/form?id=12345
    So now you need some better code to ensure that somebody doesn't use
    Code:
        www.example.com/whw/form?woof=12345&bark=hello&id=17
    So:
    Code:
    var qs = location.search;
    var rvalue = "";
    if ( qs.length > 1 ) 
    {
        var pairs = qs.split("&");
        for ( var p = 0; p < pairs.length; ++p )
        {
            var pair = pairs[p].split("=");
            if ( pair.length == 2 && pair[0].toLowerCase() == "id" ) // or whatever name=value you are looking for
            {
                rvalue = decodeURIComponent( pair[1] );    
                break; // out of the for loop
            }
        }
    }
    ... now if rvalue is not null, it is ready for use ...
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Tags for this Thread

    Posting Permissions

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