最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

JavaScript function "not defined" after importing jQuery - Stack Overflow

programmeradmin2浏览0评论

I have the following (simplified) code:

<!DOCTYPE html SYSTEM ".dtd">
<html xmlns="">
    <head>
        <title>Test</title>

        <script type="text/javascript" src=".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=".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.

I have the following (simplified) code:

<!DOCTYPE html SYSTEM "http://www.w3/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3/1999/xhtml">
    <head>
        <title>Test</title>

        <script type="text/javascript" src="http://code.jquery./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./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.

Share asked Dec 18, 2012 at 12:38 pajevicpajevic 4,6674 gold badges42 silver badges76 bronze badges 3
  • Just close your script tag...! – suresh gopal Commented Dec 18, 2012 at 12:42
  • 1 hey man there is no need of writing language="javascript" because type already specifies it! – Talha Akbar Commented Dec 18, 2012 at 12:43
  • I don't think I ever got som many answers so quickly before :) Thanks guys! – pajevic Commented Dec 18, 2012 at 12:56
Add a ment  | 

4 Answers 4

Reset to default 7

You need to close the <script> tag fully.

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

Works great on jsfiddle after changing: http://jsfiddle/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.

You have to close the script tag in the right way

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

(note the closing </script> tag)

   <script type="text/javascript" src="http://code.jquery./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!

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...

发布评论

评论列表(0)

  1. 暂无评论