2

I have the following (simplified) code:

<!DOCTYPE html SYSTEM "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
 <head>
 <title>Test</title>
 <script type="text/javascript" src="http://code.jquery.com/jquery-latest.js" />
 <script language="javascript" type="text/javascript">
 function testFunction() {
 alert('It works');
 }
 </script>
 </head>
 <body>
 <form method="post" action="test.html" onsubmit="testFunction()">
 <input type="submit" value="Test" />
 </form>
 </body>
</html>

My testFunction() function works fine by it self, that is until I import jQuery with the line

<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js" />

Now it fails and tells me that Uncaught ReferenceError: testFunction is not defined

I am hoping this is a newbie mistake and that I am missing something obvious. Notice that I haven't even try to use jQuery yet.

asked Dec 18, 2012 at 12:38
3
  • Just close your script tag...! Commented Dec 18, 2012 at 12:42
  • 1
    hey man there is no need of writing language="javascript" because type already specifies it! Commented Dec 18, 2012 at 12:43
  • I don't think I ever got som many answers so quickly before :) Thanks guys! Commented Dec 18, 2012 at 12:56

4 Answers 4

7

You need to close the <script> tag fully.

<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>

Works great on jsfiddle after changing: http://jsfiddle.net/PVYM9/

Also, in HTML5 you can shorten the doctype to just <!DOCTYPE html>, and you don't need to define type properties for your <script> tags. See jsfiddle for example.

answered Dec 18, 2012 at 12:40
Sign up to request clarification or add additional context in comments.

Comments

2

You have to close the script tag in the right way

<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>

(note the closing </script> tag)

answered Dec 18, 2012 at 12:41

Comments

1
 <script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script> // close the script tag properly
 <script type="text/javascript"> // there is no need of writing language="javascript"
 function testFunction() {
 alert('It works');
 }
 </script>

hey man there is no need of writing language="javascript" because type already specifies it!

answered Dec 18, 2012 at 12:41

Comments

1

Despite being valid XML, many HTML elements (such as script, textarea etc) don't work well in most browsers if you close them using a trailing slash (i.e. <element />). You need to create open and close tags for them to work (<element></element>). That seems to be true for XHTML as well.

Others, like your input tag, work fine the way it is. I don't know why some require a closing tag and others not, neither a list of those elements, I just know some of them from experience...

answered Dec 18, 2012 at 12:47

1 Comment

It never occurred to me that this might be the issue, although now that you mention it I do remember having the same problem with a textarea tag a long time ago.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.