Using query string to create link

Discussion in 'Javascript' started by Dr. Oz, Mar 28, 2005.

  1. Dr. Oz

    Dr. Oz Guest

    Hi,

    I am trying to read in a query string from one page and build a link to
    another page based on the query string.

    Here's the code I am using to read in the query string:

    <script "text/javascript">
    function getQueryVariable(variable) {
    var query = window.location.search.substring(1);
    var vars = query.split("&");
    for (var i=0;i<vars.length;i++) {
    var pair = vars.split("=");
    if (pair[0] == variable) {
    return pair[1];
    }
    }

    }
    </script>

    This works fine.

    I can print out the query sting value using document.write:
    document.write( getQueryVariable("x") );

    But when I try to add the query string to a link using document.write
    it doesn't work:

    document.write("<a href='prod_request.html?id=' &
    getQueryVariable('x')>Prod | </a>")
     
    Dr. Oz, Mar 28, 2005
    #1
    1. Advertising

  2. Dr. Oz wrote:

    > But when I try to add the query string to a link using document.write
    > it doesn't work:
    >
    > document.write("<a href='prod_request.html?id=' &
    > getQueryVariable('x')>Prod | </a>")


    Several problems.

    (1) To join two strings in JavaScript you use a plus sign. An ampersand is,
    I assume, a bitwise AND operator.

    (2) You didn't close your first string, nor open your last

    (3) You don't have anything to join the last string to the output of the
    getQueryVariable function.

    You probably want something like:

    document.write('<a href="prod_request.html?id=' + getQueryVariable('x') +
    '">Prod | </a>');

    (And as a couple of side notes. First, it looks like you might be depending
    on JavaScript, this isn't a good idea - especially when you can achieve the
    same effect with 100% reliability on the server side. Secondly, if you want
    a list of links, it is far better to mark them up as a list (i.e. with
    <ul>/<ol> and <li>) and them style them rather then putting pipe characters
    between them. Its better food for search engines, screen readers, etc.)


    --
    David Dorward <http://blog.dorward.me.uk/> <http://dorward.me.uk/>
    Home is where the ~/.bashrc is
     
    David Dorward, Mar 29, 2005
    #2
    1. Advertising

  3. Dr. Oz wrote:

    > But when I try to add the query string to a link using document.write
    > it doesn't work:
    >
    > document.write("<a href='prod_request.html?id=' &
    > getQueryVariable('x')>Prod | </a>")


    To concatenate strings, use the `+ยด operator:
     
    Dietmar Meier, Mar 29, 2005
    #3
  4. Dr. Oz

    RobG Guest

    Dr. Oz wrote:
    > Hi,
    >
    > I am trying to read in a query string from one page and build a link to
    > another page based on the query string.
    >
    > Here's the code I am using to read in the query string:
    >
    > <script "text/javascript">
    > function getQueryVariable(variable) {
    > var query = window.location.search.substring(1);
    > var vars = query.split("&");
    > for (var i=0;i<vars.length;i++) {
    > var pair = vars.split("=");
    > if (pair[0] == variable) {
    > return pair[1];
    > }
    > }
    >
    > }
    > </script>
    >
    > This works fine.


    You don' tell us what "works" means, what the input is or what the
    output might look like. For me, the above function creates 'query'
    as an empty string.

    >
    > I can print out the query sting value using document.write:
    > document.write( getQueryVariable("x") );
    >
    > But when I try to add the query string to a link using document.write
    > it doesn't work:
    >
    > document.write("<a href='prod_request.html?id=' &
    > getQueryVariable('x')>Prod | </a>")


    I'm not sure whether you intended this line to break where it did,
    since it is invalid JavaScript anyway, but please break lines of
    code manually at about 70 characters. Automatic breaks are nearly
    always in the 'wrong' place and cause errors when attempting to fix
    them.

    You don't say what you expect the above line to achieve. I'll guess
    that you expect document.write to put the value of
    getQueryVariable('x') into the URI at when the page loads.

    By using double quotes on the 'outside' of your expression, anything
    internal that is quoted with single quotes remains as part of the
    string. 'getQueryVariable()' is not executed but written literally
    as a string to the page.

    I prefer to use single quotes in JavaScript and double in HTML (just
    personal preference so I can easily understand what I am doing).
    Here is my version of your document.write line (presuming that the
    '&' was intended as a concatenation character and not part of the
    string):

    document.write('<a href="prod_request.html?id="
    + getQueryVariable('x') + '">Prod | </a>');

    This will pass 'x' as a character to the function and the result will
    be appended to the the href attribute string.

    Some people prefer to write with all the one type of quote and escape
    internal quotes:

    document.write("<a href=\"prod_request.html?id="
    + getQueryVariable("x") + "\">Prod | </a>");

    Choose your poison.

    On the other hand, if you expect getQueryVariable() to run when the
    link is clicked, you need to attach it to the <a> element's onclick
    event.


    --
    Rob
     
    RobG, Mar 29, 2005
    #4
    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. Kevin Spencer

    Re: Link Link Link DANGER WILL ROBINSON!!!

    Kevin Spencer, May 17, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    941
    Kevin Spencer
    May 17, 2005
  2. Roland Hall

    ASP LIKE query using parameterized query

    Roland Hall, Jan 12, 2007, in forum: ASP General
    Replies:
    8
    Views:
    232
    Roland Hall
    Jan 13, 2007
  3. roadrunner
    Replies:
    1
    Views:
    263
    Gunnar Hjalmarsson
    Feb 8, 2006
  4. pavi
    Replies:
    0
    Views:
    1,386
  5. nick
    Replies:
    1
    Views:
    511
    David Mark
    Feb 13, 2011
Loading...

Share This Page