[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Call Javascript from a different file

Posted on 2007-10-11
10
Medium Priority
?
1,542 Views
Last Modified: 2013-12-13
Hello I want to call the following file from a different file:

<script language = "Javascript">
function validateForm(){

      var VCity=document.Form1.City
      var VState=document.Form1.State

      if ( VCity.value  == "") {
            VCity.value = ""
          alert( "Please fill in a city" )
              VCity.focus()
          return false;
      }
      
      
      if ( VState.value == "") {
            VState.value = ""
          alert( "Please fill in a state" )
              VState.focus()
          return false;
      }
      
    return true
}
 </script>



It validates a form.  I tried to name it validate.js and call it with the following:

<script language="JavaScript" type="text/javascript" src="graphics/validate.js"></script>

Do you know why this happens or how to fix it?  Thanks!
0
Comment
Question by:smoothcat11
  • 2
  • 2
  • 2
  • +3
9 Comments
 
LVL 23

Accepted Solution

by:
basicinstinct earned 672 total points
ID: 20054927
take the script tags out of validate.js
ie, the contents of that file should just be:


function validateForm(){

      var VCity=document.Form1.City
      var VState=document.Form1.State

      if ( VCity.value  == "") {
            VCity.value = ""
          alert( "Please fill in a city" )
              VCity.focus()
          return false;
      }
     
     
      if ( VState.value == "") {
            VState.value = ""
          alert( "Please fill in a state" )
              VState.focus()
          return false;
      }
     
    return true
}
0
 
LVL 14

Expert Comment

by:Jai S
ID: 20054932
you should not use text...jst use this alone
<script language="JavaScript" src="graphics/validate.js"></script>

and make sure that your file is in the specified folder
0
 
LVL 23

Expert Comment

by:basicinstinct
ID: 20054943
@jaiganeshsrinivasan

actually it is the 'language' attribute that has been deprecated, the 'type' attribute is the w3c standard way to do it, but there is no harm having both... if either of them were to go it should be 'language'
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 39

Assisted Solution

by:Pratima Pharande
Pratima Pharande earned 664 total points
ID: 20055102
<script language="JavaScript" type="text/javascript" src="graphics/validate.js"></script>

make sure path is right

For above code if your calling file must be in the root directory and
js file like below

and root/graphics/validate.js

Can you share your diectory structure with us ?

what error you are getting ?
0
 
LVL 14

Expert Comment

by:Jai S
ID: 20055129
i think Business Intelligence(sorry - Basic Instinct) is right...the file should not contain the script tag...becos it is in the script tag that we are specifying the content....apologies...
0
 
LVL 5

Expert Comment

by:siva_siva
ID: 20055320
Use semicolon in all the line..

function validateForm(){
      var VCity=document.Form1.City;
      var VState=document.Form1.State;

      if ( VCity.value  == "") {
            VCity.value = "";
          alert( "Please fill in a city" );
              VCity.focus();
          return false;
      }
           
      if ( VState.value == "") {
            VState.value = "";
          alert( "Please fill in a state" );
              VState.focus();
          return false;
      }
     
    return true;
}
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 20056378
siva_siva: As with the type attribute - not important.
Semicolons are only mandatory in event handlers like
onClick="var x=2
alert(x)"

which will not work unless you add ; after x=2

Also two statements on one line needs semicolon between them:

function alertTwo() {
  alert(a); alert(b)
}
0
 
LVL 75

Assisted Solution

by:Michel Plungjan
Michel Plungjan earned 664 total points
ID: 20056427
@smoothcat11 : once you have fixed the file, consider passing the form to the function using the "this" keyword
Also why set the field to blank when it is blank???:

<form onSubmit="return validate(this)">

with the script looking like this

function validateForm(theForm) {
  var VCity=theForm.City;
  if ( VCity.value  == "") {
    alert( "Please fill in a city" )
    VCity.focus()
    return false;
  }
  var VState=theForm.State;
  if ( VState.value == "") {
    alert( "Please fill in a state" )
    VState.focus()
    return false;
  }
  return true
}


Michel.

PS: Points to BasicInstincts if you still had script tags in the external file
0
 
LVL 1

Expert Comment

by:Computer101
ID: 20475706
Forced accept.

Computer101
EE Admin
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
The title says it all. Writing any type of PHP Application or API code that provides high throughput, while under a heavy load, seems to be an arcane art form (Black Magic). This article aims to provide some general guidelines for producing this typ…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
How to create a custom search shortcut to site-search Experts Exchange using Google in the Firefox browser. This eliminates the need to type out site:experts-exchange.com whenever you want to search the site. Launch your Bookmark Menu: Press 'Ctrl +…
Suggested Courses
Course of the Month19 days, 15 hours left to enroll

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question