473,375 Members | 1,542 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,375 software developers and data experts.

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 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
3 7421
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Bob | last post by:
Currently I am using this statement to translate 3 fields in my db thru Visual Basic. I import the data from one table to another then call the IFF statements and the NewDate to translate the...
3
by: Random Person | last post by:
Does anyone know how to use VBA to relink tables between two MS Access databases? We have two databases, one with VBA code and the other with data tables. The tables are referenced by linked...
1
by: Daveyk0 | last post by:
Hello there, I have a front end database that I have recently made very many changes to to allow off-line use. I keep copies of the databases on my hard drive and link to them rather than the...
1
by: anshul | last post by:
Can somebody tell me about state management in asp.net using Query Strings. I am just unable to understand this. Anshul
3
by: Hitesh | last post by:
Hi, I am getting the response from another Website by using the HttpHandler in my current site. I am getting the page but all the images on that page are not appearing only placeholder are...
4
by: Oceania | last post by:
Hi All, I did try using the sample code from the ADO.net forum to link one table, it is working fine. But, I got a problem when I tried to link all tables (20 tables)... Please help, thank...
2
by: Randy | last post by:
I am trying to relink some Oracle tables in an Access database via VB.NET and ADOX. I receive the following error when executing the cat.ActiveConnection link "Arguments are of the wrong type,...
4
by: Rick | last post by:
Access2003 in XP I'm using the code below to append any new records from (tbl_From_Mainframe) into (tbl_Appended_Data). It takes more than a minute to search 7000 records for a dozen new records....
221
Atli
by: Atli | last post by:
You may be wondering why you would want to put your files “into” the database, rather than just onto the file-system. Well, most of the time, you wouldn’t. In situations where your PHP application...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.