How to "include" javascript file within asp file

Results 1 to 3 of 3

Thread: How to "include" javascript file within asp file

  1. #1
    Join Date
    Dec 1969

    Default How to "include" javascript file within asp file

    Hello,<BR>Can someone tell me why I couldn&#039t include the javascript file within .asp page? It keeps giving me the error. For example, in my index.asp file, I include the file "valiadation.js" like:<BR>&#060;script language="JavaScript" src="valiadation.js"&#062;&#060;/script&#062;<BR><BR>The "valiadation.js" contains the checkform() function that will check all the fiels name from within the form. If the users don&#039t fill out the form completely, it&#039ll prompt the window message to remind them to fill in the blank before they can submit the form. The problem is that it will work fine if I code my checkform() function (javascript) inside my index.asp file, instead of using the include file. But I want to include the valiadation.js file within my index.asp so it&#039s easy to maintain. Please help. I even tried to change the extention of valiadation.js into, and used the include method such as:<BR>&#060;!-- #include file="" --&#062;<BR><BR>Unfortunately, it still doesn&#039t work. Can you tell me what I shall change in order to make it works. Thank you so much. <BR><BR>p.s: Also, in my checkform() function, is this posible to put all the fields name into an array so I can check all the fields name? Right now I have to check every single field with using if else statement. It&#039s very painful because I have a lot of fields in my form. Can you tell me the most sufficient way to check the form? Thanks. Below is my checkform() function which I think isn&#039t the best way. <BR>&#060;script language="JavaScript"&#062;<BR>&#060;!--<BR>function checkform() {<BR>// check fields for data<BR>if (document.frmUser.txtName == "") {<BR>alert("Please enter your Name");<BR>}<BR>else if (document.frmUser.txtEmail == "") {<BR>alert("Please enter your Email");<BR>}<BR>else if (document.frmUser.txtAdd == "") {<BR>alert("Please enter Address");<BR>}<BR> else document.frmUser.submit();<BR>}<BR>// --&#062;<BR>&#060;/script&#062;<BR><BR>

  2. #2
    jason Guest

    Default RE: How to

    you&#039ve left out runat="server" for a start, but the best way is probably to use the .inc method (but rename it to .asp for security reasons)<BR><BR>jason

  3. #3
    Matt G. Guest

    Default Solutions...

    As for your first question, I saw nothing wrong with your code... My guess is you have mis-spelled the filename or have the wrong path. Here is a piece of my own code from a page on the &#039Net that uses a Javascript include that I know works:<BR><BR>&#060;SCRIPT SRC="../includes/tfc.js" LANGUAGE="JAVASCRIPT" TYPE="TEXT/JAVASCRIPT"&#062;&#060;/SCRIPT&#062;<BR><BR>Also, including an external .js file has nothing to do with ASP and ASP #Includes (though, of course, you could Include a .js file, or any file, through ASP)...<BR><BR>As for your second question about your form checking, you can use an array to rapidly check the elements of your form. In fact, Javascript automatically creates an array for each element type (radio buttons, check boxes, whatever) in your form, so it is even easier to validate this way! That said, I don&#039t recommend it. Use individual names for clean and easy to follow code.<BR><BR>I am including my own "checkform" function. Unlike yours, it will only give you one alert for ALL of the errors your form has (clicking OK to 25 alert boxes gets annoying!) and it will set the focus to the first error field you have... I think you&#039ll be able to figure out how it works. It is different than yours in that you will have to pass the form itself to the function from a regular button (which you could label SUBMIT -- but it should be a regular button that has an onClick="submitForm(this.form);" with it)...<BR><BR>Repsectfully,<BR><BR>Matt G.<BR><BR>HERE&#039S THE CODE ------------------------<BR><BR> // The Javascript in this section is all originally coded by Matt G.<BR><BR> // ================================================== =====================<BR> // CHECK FORM BEFORE SUBMITTING<BR> // ================================================== =====================<BR> function submitForm(xForm)<BR> {<BR> // The form is sent here, and if no errors<BR> // are found it is submitted. If errors are found, a running tally<BR> // is kept so the user can get a message listing all of the errors...<BR><BR> var errorMsg = new Array() // a running log of all errors found<BR> var errorCount = 0 // # of errors found<BR> var errorFocus = "" // where to set focus after error found<BR> var finalMsg = "" // the final message of all errors found <BR> <BR> // Check Name<BR> if (xForm.textName.value == "")<BR> { errorCount++<BR> errorMsg[errorCount] = "
    -You must include a Name"<BR> errorFocus = "textName"<BR> } <BR> // Check Email<BR> if (xForm.textEmail.value == "")<BR> { errorCount++<BR> errorMsg[errorCount] = "
    -You must include an Email Address"<BR> if(errorFocus=="") errorFocus = "textEmail"<BR> } <BR> <BR> // Check the Error Count<BR> // If the errorCount GREATER than ZERO then ERRORS have been found...<BR> if (errorCount &#062; 0)<BR> {<BR> if (errorCount == 1)<BR> { finalMsg = "Sumbission Halted due to this error:"<BR> finalMsg = finalMsg + errorMsg[1] <BR> }<BR> else <BR> { finalMsg = "Submission Halted due to the following errors:"<BR> for (i=1; i &#060;= errorCount; i++)<BR> { finalMsg = finalMsg + errorMsg[i] }<BR> } <BR> errorFocus = "xForm." + errorFocus + ".focus()"<BR> eval(errorFocus)<BR> alert(finalMsg)<BR> }<BR> // If it reaches here, then NO ERRORS WERE FOUND! <BR> else <BR> { <BR> xForm.submit()<BR> }<BR> } <BR><BR><BR><BR><BR>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts