View Full Version : newbie: putting js in a separate file

10-28-2004, 05:10 PM
I know this question is probably very much on the "supposed-to-know" end of things.... but I'll ask anyway.

I have validation js in php forms pages on my site. The validation works fine when I have it in the form .php page but when I pull it out and put it in a separate .js file, it no longer works.

Can someone point me to what piece of knowledge I'm missing?

Here's a fragment of the code:

<script type="text/javascript">

function isBlank(testStr)
if (testStr.length == 0) // nothing entered?
{return true;}

for (var i = 0; i <= testStr.length-1; i++) // all spaces?
if (testStr.charAt(i) != " ")
{return false;}
return true;

function checkForm(form)

if (isBlank(form.firstName.value))
alert("Please enter your first name.");
return false;

The //<![CDATA[ stuff is so that it's xhtml compliant.

Also, when I do the include (putting the js in a separate file), I do it like this:

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

Any ideas? Could it be that I am using the DOM incorrectly?

10-28-2004, 05:17 PM
Any ideas? Could it be that I am using the DOM incorrectly?

if the script works within the HTML, but not in an external JS file, then it's not an error in the script per se. sounds to me more likely to be a problem with the path.

btw: when you post a message about problem scripts, it helps to tell us the error, if any, that is produced. we ain't pyshic. you gotta tell us what the error says, not just describe wot you think is happening.

if there's no error, chances are that it's not a problem with the code. as i said, sounds like a path problem!

10-28-2004, 05:20 PM
Someone correct me if I'm wrong, but are the CDATA tags necessary when calling an external .js file? Could that be part of the problem if not the path?

My understanding is that CDATA tags are only necessary if the script is inline within the XHTML.

10-28-2004, 05:30 PM
Sorry about that. It just submits the form with no alerts if data is missing.

10-28-2004, 05:33 PM
Yes, that worked! No CData should be there.

Thank you very much!

10-28-2004, 09:15 PM
You're very welcome. Javascript is not my strong suit, but I'm glad to be able to repay the help I've gotten here.