Using query string to create link

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Dr. Oz

    #1

    Using query string to create link

    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 getQueryVariabl e(variable) {
    var query = window.location .search.substri ng(1);
    var vars = query.split("&" );
    for (var i=0;i<vars.leng th;i++) {
    var pair = vars[i].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( getQueryVariabl e("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_requ est.html?id=' &
    getQueryVariabl e('x')>Prod | </a>")

  • David Dorward

    #2
    Re: Using query string to create link

    Dr. Oz wrote:
    [color=blue]
    > But when I try to add the query string to a link using document.write
    > it doesn't work:
    >
    > document.write( "<a href='prod_requ est.html?id=' &
    > getQueryVariabl e('x')>Prod | </a>")[/color]

    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
    getQueryVariabl e function.

    You probably want something like:

    document.write( '<a href="prod_requ est.html?id=' + getQueryVariabl e('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

    Comment

    • Dietmar Meier

      #3
      Re: Using query string to create link

      Dr. Oz wrote:
      [color=blue]
      > But when I try to add the query string to a link using document.write
      > it doesn't work:
      >
      > document.write( "<a href='prod_requ est.html?id=' &
      > getQueryVariabl e('x')>Prod | </a>")[/color]

      To concatenate strings, use the `+ยด operator:

      Comment

      • RobG

        #4
        Re: Using query string to create link

        Dr. Oz wrote:[color=blue]
        > 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 getQueryVariabl e(variable) {
        > var query = window.location .search.substri ng(1);
        > var vars = query.split("&" );
        > for (var i=0;i<vars.leng th;i++) {
        > var pair = vars[i].split("=");
        > if (pair[0] == variable) {
        > return pair[1];
        > }
        > }
        >
        > }
        > </script>
        >
        > This works fine.[/color]

        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.
        [color=blue]
        >
        > I can print out the query sting value using document.write:
        > document.write( getQueryVariabl e("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_requ est.html?id=' &
        > getQueryVariabl e('x')>Prod | </a>")[/color]

        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
        getQueryVariabl e('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. 'getQueryVariab le()' 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_requ est.html?id="
        + getQueryVariabl e('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_req uest.html?id="
        + getQueryVariabl e("x") + "\">Prod | </a>");

        Choose your poison.

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


        --
        Rob

        Comment

        Working...