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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to include a js function in html page? 2

Status
Not open for further replies.

wvdba

IS-IT--Management
Jun 3, 2008
465
0
0
US
hi,
i have the following code in js:
Code:
<script language="javascript"> 
function isUSCurrency (sString) {
return RegExp(/^\$?\d+(\.\d{2})?$/).test(String(sString).replace(/^\s+|\s+$/g, ""));
}
</script> 
<script language="javascript"> 
function myfunc()
{   
   var amt = document.forms['frm1'].elements['amt'].value;
   if(!isUSCurrency(amt))
     {
      alert("bad amount.");
     }
   if(isUSCurrency(amt))
     {
      alert("amouht is good.")
     } 
}
</script>
<form name="frm1" action="test_currency.html" method="post" onSubmit="return myfunc();"
<INPUT type="text" NAME="amt" id="amt" size=8>
<br><br>
 <input type="hidden" name="submit" value="submit">
 <INPUT TYPE="submit" class="GO" value="submit">
 <INPUT TYPE="reset"  class="GO" VALUE="Clear">
</form>
basically, it tests for currency. it works the way in the code above. however, i want to take the two functions and put them in a js file with include. i have tried several snippets from web to include these, but the functions do not work when i use include. if i put the code as above, it works.
any idea why this is?
thanks.

 

How are you including the file, and more importantly what does the included file look like?

Do you get any errors? If so which ones?
Both IE and FF should show you any errors that are being generated.





----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
the include file looks like this (this is the content of test.js):
Code:
<script language="javascript"> 
function isUSCurrency (sString) {
return RegExp(/^\$?\d+(\.\d{2})?$/).test(String(sString).replace(/^\s+|\s+$/g, ""));
}
</script> 
<script language="javascript"> 
function myfunc()
{  
   var amt = document.forms['frm1'].elements['amt'].value;
   if(!isUSCurrency(amt))
     {
      alert("bad amount.");
     }
   if(isUSCurrency(amt))
     {
      alert("amouht is good.")
     } 
}
</script>
in my html file, i have this code:
Code:
<script language="javascript" src="test.js" />











 
Yup, if you are including the file using the <script src=...> tag then its already assumed to be script. The <script> tags inside the JS file are not required and would be causing an error.

And you not only have 1 set of <script> tags in the JS file you have two.

Remove them all.

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
thank you both.
that did the trick. my problem was that i had two sets of <script></script>'s. one in the html page referring to the include file, and one in the include file itself.
thanks much.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top