Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Using query string to create link

Reply
Thread Tools

Using query string to create link

 
 
Dr. Oz
Guest
Posts: n/a
 
      03-28-2005
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>")

 
Reply With Quote
 
 
 
 
David Dorward
Guest
Posts: n/a
 
      03-28-2005
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
 
Reply With Quote
 
 
 
 
Dietmar Meier
Guest
Posts: n/a
 
      03-28-2005
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:

 
Reply With Quote
 
RobG
Guest
Posts: n/a
 
      03-28-2005
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to convert url with query string to url without query string nick Javascript 1 02-13-2011 11:20 PM
CGI - How to use upload_hook and query the query string ? roadrunner Perl Misc 1 02-08-2006 01:50 AM
I want to create a link "e-mail this page to a friend" on clicking this link i want to send the URL of that current page to a friend pavi Javascript 0 01-13-2006 12:10 PM
RE: Link Link Link =?Utf-8?B?REw=?= Windows 64bit 0 05-17-2005 12:15 PM
Re: Link Link Link DANGER WILL ROBINSON!!! Kevin Spencer ASP .Net 0 05-17-2005 10:41 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57