• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 287
  • Last Modified:

passing a variable through an href statement

How do I pass a javascript variable when I use an HREF statement?  I need to pass a variable "instr" to a cgi.

I tried that and the value seems to get lost.
1 Solution
Are you doing this

<a href="/bin/file.cgi?instr=some_instruction">

or this

<script language="javascript">
Try this...........

x="/bin/file.cgi?instr=" & instr

<A href="<%=x%>">Lets Go</a>

Works for me, Give it a try........

If you want it to be from an html link, you can combine the two methods that jimmyontour mentioned like so:

<A HREF="JavaScript:void(0);" onClick="goto(); return false;">jkashd</A>

<script language="javascript">
function goto()
{// any manipulation of variable instr here...
// -->

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

This is what you must do:

<a href="javascript:document.location='/bin/file.cgi?instr=' + escape(instr)">the link</a>


PS: None of the above used escape and this is important.
kllewelynAuthor Commented:
basically I'm doing this based on a click


I want the or_main frame to be replaced with the or_image.cgi. The cgi needs to receive the instr variable and do a database select statement.  I have an alert right before I use the above statement, and the variable equals what it should, but when it gets to the cgi, the passed value is blank.  I must not be passing it correctly.
Which frame is this being called from?  If it's "or_vars", then you don't need to reference the variable via top.or_vars...  It should work though.  Are you sure that or_vars is the correct frame name?  Is instr a variable in that frame or an object like a text field?  It's a text field it should be:


P.S. It is important to escape this variable as xabi said.  I had forgotten about that...  If it's not escaped, the value sent to the cgi may not be correct ly if invalid characters like spaces are used in instr.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now