Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to pass VBScript var to JavaScript function in ASP 1

Status
Not open for further replies.

calandrelli

Technical User
Jun 14, 2002
69
0
0
US
I have a pop-up date picker that is written in Javascript that will take one of it's parameters as a default date. I use it on two pages... On my Add a record page the default value is today but on my edit-record-page I need it to reflect the date already stored for that record. The “Add” page works fine but the edit page uses a SQL statement retrieve a value from the database and stored it in a VBScript variable. I can’t seem to pass that variable on the JavaScript function that creates the calendar.
Code:
VBSCRIPT: 

...query = "Select * from tbl_Main WHERE Request_Nbr = " & key recordset.Open query, connec...

Due_Date = recordset("Due_Date")

Code:
JAVASCRIPT: 

<script type="text/javascript" src="calendarDateInput.js"></script>

<script> DateInput(Param 1, Param 2, <%=Due_Date%>) </script>

NOTE: The function DateInput is run by a separate js file.
The complete code for the calendar is located here...
Jason's Calendar


I keep getting the error of Due_Date is undefined.
 
Then you have an ASP problem, not Javascript. Do you need to have

Dim Due_Date

at the top of your ASP code? What line of the code does the error indicate is causing the problem?

Lee
 
If the Javascript code you provided in your original question is in an external Javascript file, it won't work. The external file isn't loaded until the page is on the client computer and all ASP code has been processed.

Lee

 
Trollacious, The following code is loaded as part of the head and the remainder is in the body.

<script type="text/javascript" src="calendarDateInput.js"></script>
 
I do have DIM Due_Date at the top of the code and if I create a text field with the value set to <%=Due_Date%> the test box displays the value 03/03/1970.

I am stumped...
 
Just to be clear though you probably know this.
All of the ASP code will execute prior to any of the Javascript running or HTML being rendered.

So the first thing on your page that happens is all of your VBScript code executes which should be returning your recordset value to the VBScript variable Due_Date.

Then you should load your .js file that contains your Javascript functions. Then you can make the call to the function passing the Due_Date value as you show it above.

The one thing I can think of is that the date comes across as a text value and you are not putting it within quotes in your function call. Try the call like this:
<script> DateInput(Param 1, Param 2, '<%=Due_Date%>') </script>

If that does not do it then somehow the value is not being set prior to the call to the function. Perhaps a different portion of your code also makes a call to DateInput function? I suspect the quotes will help though.


Paranoid? ME?? Who wants to know????
 
Thanks for the information. The single quotes didn't work exactly but they got me on the correct path.

Code:
yourjsvar = "<%=Due_Date%>";
DateInput('Field', true, 'MM/DD/YYYY', yourjsvar);


 
If you are still having problems with it post the code and maybe we can help further.

When you run into a situation like this the best thing to do is to simplify your code as much as possible. Create a test version that has everything else stripped out so you have just the very basics to replicate the issue. It makes troubleshooting a whole lot easier. Often you find that when pulling out other sections of code the problem goes away also for unexpected reasons that you might have spent a long time trying to figure out.

Good luck.


Paranoid? ME?? Who wants to know????
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top