Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    ew1
    ew1 is offline
    New Coder
    Join Date
    Mar 2004
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question newbie: putting js in a separate file

    Hi,
    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:

    Code:
    <script type="text/javascript">
    //<![CDATA[
    
    
    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.");
    		form.firstName.focus();
    		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:

    Code:
    <script src="regRadioOnly_noDB_val.js" type="text/javascript"></script>
    Any ideas? Could it be that I am using the DOM incorrectly?

  • #2
    Senior Coder
    Join Date
    Feb 2004
    Location
    Edinburgh
    Posts
    1,352
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by ew1
    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!
    *keep it simple (TM)

  • #3
    Regular Coder
    Join Date
    Jul 2004
    Location
    mile high city
    Posts
    482
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.
    Computer, kill Flanders... Did I hear my name? My ears are burning...
    Good start. Now finish the job.

  • #4
    ew1
    ew1 is offline
    New Coder
    Join Date
    Mar 2004
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Sorry about that. It just submits the form with no alerts if data is missing.

  • #5
    ew1
    ew1 is offline
    New Coder
    Join Date
    Mar 2004
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts
    mcdougals4all,
    Yes, that worked! No CData should be there.

    Thank you very much!

  • #6
    Regular Coder
    Join Date
    Jul 2004
    Location
    mile high city
    Posts
    482
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.
    Computer, kill Flanders... Did I hear my name? My ears are burning...
    Good start. Now finish the job.


  •  

    Posting Permissions

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