Using query string to create link

D

Dr. Oz

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>")
 
D

David Dorward

Dr. Oz said:
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.)
 
D

Dietmar Meier

Dr. Oz said:
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:
 
R

RobG

Dr. Oz said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,526
Members
44,997
Latest member
mileyka

Latest Threads

Top