By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
426,179 Members | 2,175 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 426,179 IT Pros & Developers. It's quick & easy.

Using query string to create link

P: n/a
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[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( 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>")

Jul 23 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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
Jul 23 '05 #2

P: n/a
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:

Jul 23 '05 #3

P: n/a
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[i].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
Jul 23 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.