View Full Version : JavaScript ASP Page: Custom Function Is Not Defined

09-02-2005, 10:24 PM
I apologize if this is a bit general or high-level, but I'm stumped and don't know what to do. I've created a function that works fine under straight JavaScript, but fails when used in an ASP context.

I've researched JavaScript in ASP pages and the syntax seems to be correct. I don't know what to do to resolve this. I see an error in the JavaScript Console that tells me the "helpdesk" function is undefined.

I have four menu buttons that are keyed to execute this function onClick. Here's one example of a button:

<CODE><INPUT TYPE=image NAME="DBNHD" SRC="DBButton.jpg" BORDER=0 ONCLICK="helpdesk('DB')"></CODE>

The function, "helpdesk", is supposed to check the value of the menu button against switch cases and then adjust the look of the page depending on the group selected. "Helpdesk" also calls an "updates" function that hides or reveals Response.Write statements that display group-specific information.

The following scripting is defined in the header, and I am using a <%@ LANGUAGE="JavaScript"%> tag at the top of my code.

Help will be greatly appreciated.

var menu;
var contract;

var DBConn = Server.CreateObject("ADODB.Connection");
var UpdateRecord = Server.CreateObject("ADODB.Recordset");
var SQLQuery = "SELECT DBRecent.UserID, DBRecent.Subject, DBRecent.Update, DBRecent.Affected, DBRecent.Actions, DBRecent.Info, DBRecent.Date, DBRecent.Time FROM DBRecent;";
UpdateRecord.Open(SQLQuery, DBConn);

function helpdesk (group) {var archive;
var defmsg = document.getElementById("defdisplay");
var update = document.getElementById("updtdisplay");
if (menu != null && menu != "default") {menu.style.display = "none"};
switch (group) {case "DB":
contract = "DB";
menu = document.getElementById("DB");
archivet = "Dressbarn Archive";
case "FAI":
menu = document.getElementById("FAI");
contract = "FAI";
archivet = "FAI Archive";
case "FNC":
menu = document.getElementById("FNC");
contract = "FNC";
archivet = "FNC Archive";
case "MIS":
menu = document.getElementById("MIS");
contract = "MIS";
archivet = "MIS Archive";
if (menu != null && menu != "default") {menu.style.display = ""};
if (contract != null && contract != "default") {updates(true)}
else {updates(false)};
var archive = document.getElementById("archiveheader");
archive.childNodes[0].nodeValue = archivet};

function updates (state) {var defmsg = document.getElementById("defdisplay");
var update = document.getElementById("updtdisplay");
if (state == true) {defmsg.style.display = "none"; update.style.display = ""}
else {defmsg.style.display = "table-row"; update.style.display = "none"}}

09-03-2005, 04:27 AM
Everything between <% and %> is server-side. It seems you want client side action to take place. The functions are no longer available to you once the page has been served. Move the functions to the <script></script> tags and see if that gives you the functionality you want.


09-03-2005, 05:00 AM
I need to pass the value for the variable "contract" from the JavaScript to the ASP in order to change the information displayed.

This page is designed to be a "Hot News" page for my Helpdesk staff. We have four contracts that receive separate updates. For ease of use, I've created a database and input form for my team leads to add updates. The most recent update is then displayed on a contract-specific basis.

Any guidance here would be helpful. This stuff is driving me nuts. Every time I fix one function I break another. It seems there are dozens of different opinions on ASP and JavaScript syntax, and at least five different ways to correctly write each function.

I miss the days of structured programming languages.

09-03-2005, 06:27 AM
What i have in mind is creating a hidden field.
When s.o click a button, javascript will copy value to that field and submit for asp to process.

09-04-2005, 05:43 AM
I need to pass the value for the variable "contract" from the JavaScript to the ASP in order to change the information displayed.

I am not sure what you mean by this statement. As you have it written now the javascript is your ASP. Sort of anyway, as I stated before, it will not do what you want because you are looking for client side manipulation. When the button is clicked there is no function to call. All the code between the <%%> was run on the server prior to serving the page to the client and that code does not get sent with the page. On the client side you can change the display as you wish based on the value passed into the function(s), but the function(s) will need to be inside <script></script> tags so that the client has access to it. Your code doesn't seem to be the issue, but the way you are implementing is.


09-04-2005, 03:58 PM
Your code will not work on server side jscript because there is no document object in server side jscript, it is only a part of client side javascript/jscript.

as Bald eagle stated you have to remember that asp is finished long before any events like the clicking of a button, or the selection of a dropdown are triggered by the user. You can add a clientside function to trigger a postback event that will then run your code, but you will need to remove any reference on the server side code to the document and the elements of it.

09-06-2005, 12:09 PM
Ok I am not too good in server side Javascript

As i understand 1. u need to run asp script when usew click ?
then add hidden fields as said above
or store what u want in cookies

and 2. if u want to pass something on javascript from asp then

in braket helpdesk ('<%=urValue%>')


09-06-2005, 12:50 PM
As BaldEagle mentioned, the two functions (helpdesk and updates) should be on the client side of the page. dont see the whole page but you have to move the "%>" sign to the line just below the line which says: UpdateRecord.Open(SQLQuery, DBConn);

That way your functions are not anymore at the serverside part of the page. now you have to put a <script> tag before and a </script> tag after the functions (but they might be on the page already) then things should be better....

09-07-2005, 12:48 AM
Thank you to everyone who pitched in.

I ended up making server-side scripting to post all of the data I needed to SPAN and TABLE elements that were set STYLE="display: none". When a user clicks the apropriate button, the client-side scripting changes the "display" equal to null and the data appears.

While this method is crude, and created another 20 lines of code, it works.

Thanks to Miranda and BaldEagle for reminding me that ASP is server-side, and parsed before the page is ever displayed. That was the simple, painfully obvious thing I was overlooking.

Sorry to bother everyone with such a "newbie" issue.

02-19-2009, 06:49 PM
Thank ya'll. This post was very helpful for a newbie like me.