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

JavaScript function is not defined - Stack Overflow

programmeradmin5浏览0评论

Consider:

File script.js,

    function AdaugaComboBox(id, name){
        var select_tag = document.getElementById(id);
        select_tag.innerHTML += "<select name='"+name+"'><option value='0'>Selecteaza autor</option></select><br/>";
        return true;
    }

and file index.html:

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

    <body>
        <table>
        <tr>
            <td id="autori">...</td>
        </tr>
        <tr>
            <td>
                <input type="button"
                       value="Adauga autor"
                       onclick="AdaugaComboBox('autori', 'autori[]')"/>
            </td>
        </tr>
        </table>
    </body>
</html>

The scope of the function is to add a bo box to the specific TD in the TABLE. But when I press the button this error appears:

AdaugaComboBox is not defined

Why?


Update:

!!! I've fixed it. The problem was with another function.

Consider:

File script.js,

    function AdaugaComboBox(id, name){
        var select_tag = document.getElementById(id);
        select_tag.innerHTML += "<select name='"+name+"'><option value='0'>Selecteaza autor</option></select><br/>";
        return true;
    }

and file index.html:

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

    <body>
        <table>
        <tr>
            <td id="autori">...</td>
        </tr>
        <tr>
            <td>
                <input type="button"
                       value="Adauga autor"
                       onclick="AdaugaComboBox('autori', 'autori[]')"/>
            </td>
        </tr>
        </table>
    </body>
</html>

The scope of the function is to add a bo box to the specific TD in the TABLE. But when I press the button this error appears:

AdaugaComboBox is not defined

Why?


Update:

!!! I've fixed it. The problem was with another function.

Share Improve this question edited Sep 19, 2019 at 7:12 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Jan 27, 2010 at 12:30 EmanuelEmanuel 6,97220 gold badges60 silver badges79 bronze badges 6
  • 2 script.js is not included anywhere in your HTML... – Langdon Commented Jan 27, 2010 at 12:32
  • Where in the document tree is the function defined? – Jacob Relkin Commented Jan 27, 2010 at 12:32
  • The script.js is included in the document and I get the same error. – Emanuel Commented Jan 27, 2010 at 12:40
  • 1 The code that you have posted works fine for me in Chrome and IE - no errors. If this is a shortened sample of your code, the error must be somewhere else. – Andy E Commented Jan 27, 2010 at 12:46
  • I bet your script.js file isn't in a "js" sub directory, because it works fine. – Langdon Commented Jan 27, 2010 at 12:50
 |  Show 1 more ment

3 Answers 3

Reset to default 4

If the script is included in your HTML, then it's possible that you don't have the path correct based on the location of the HTML file. Check with Firefox/Firebug to make sure that the JS file is being downloaded correctly.

Your HTML should be:

<html>
<head>
     <script src="script.js" type="text/javascript"></script> 
</head>
<body>
<table>
<tr>
  <td id="autori">...</td>
</tr>
<tr>
  <td>
     <input type="button" value="Adauga autor" onclick="AdaugaComboBox('autori', 'autori[]')"/>
  </td>
</tr>
</table>
</body>
</html>

You have to put a reference to the script.js file.

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

发布评论

评论列表(0)

  1. 暂无评论