未捕获的ReferenceError:未定义函数

Uncaught ReferenceError: function is not defined

本文关键字:未定义 函数 ReferenceError      更新时间:2023-09-26

我在调用外部.js文件中声明的函数时遇到问题。我已经在主页中包含了.js文件,但显然它是部分工作的(只有一个函数被正确调用,其他函数生成一个"Uncaught ReferenceError:函数未定义")

这是Js文件:`

<script type="text/javascript">
var maxAmount = 170;
// Modify the counter textField by given the textField to control and his counter
function textCounter(id_Tf, id_Cd) {
    // Method that is called succesfully
    var element = document.getElementById(id_Tf);   
    var nameLenght  = element.value.length;    
    var countDisplay = document.getElementById(id_Cd);  
    if(nameLenght <= maxAmount){
        countDisplay.value = maxAmount - nameLenght;
    }
    else{
        countDisplay.value = "0";
}

function titleShow(){
    theTitle = val('title').replace(/^'s+|'s+$/g,"");
    get('out_title').innerHTML = theTitle;
    if(get('check_bold').checked == true){
        highlightTerms('out_title');
    }
}
function snippetShow(){
    console.log("I've entered here");
    theSnippet = val('description').replace(/^'s+|'s+$/g,"");
    if(theSnippet.length + dateLength <= 156){
        get('out_snippet').innerHTML = theSnippet;}
    else{
        var snipLimit = 153 - dateLength;
        snippetSpace = theSnippet.lastIndexOf(" ",snipLimit);
        get('out_snippet').innerHTML = theSnippet.substring(0,snippetSpace).concat(ellipsis.bold());}
}
function urlFunction(){
    var theURL = val('in_url');
    theURL = theURL.replace('http://','');
    theURL = theURL.replace(/^'s+|'s+$/g,"");
    get('out_url').innerHTML = theURL;
    if(get('check_bold').checked == true){
        highlightURL();}}

这是主页:'

<?php
    include('Code/Php_code.php');
    include('Code/Js_code.js');
?>
<!DOCTYPE html>
<html>
     <head>
        <title><?php echo $title ?></title>
     </head>
     <body>
        <form action="Database_Interface.php" method="post"><br>
          Title:<br>
          <input type="text" id="title" name="title" size="150" maxlength="150" value="<?php echo $title ?>" onKeyUp='textCounter("title","countDisplay");'><br>
          <input readonly type="text" id="countDisplay" size="3" maxlength="3" value="150"> Characters Remaining
          <br><br>
          Description:<br>
          <input type="text" id="description" name="description" size="150" value="<?php echo $tags['description'] ?>" onKeyUp='textCounter("description","countDisplay2");'><br>
          <input readonly type="text" id="countDisplay2" size="3" maxlength="3" value="150"> Characters Remaining
          <br><br>
          Keywords:<br>
          <input type="text" id="keywords" name="keywords" size="150" value="<?php echo $tags['keywords'] ?>" onKeyUp='textCounter("keywords","countDisplay3");'><br>
          <input readonly type="text" id="countDisplay3" size="3" maxlength="3" value="150"> Characters Remaining
          <br><br>
          <input type="submit" value="Carica sul database">
          <input type="button" value="see" onclick='snippetShow();'>
    </form><br>

    <div style="background-color:#f2f2f2; border:1px solid; border-color: black; position:relative;">
        <h3><a href="#" onclick="return false;" class="l"><span id="out_title"></span></a></h3>
        <div>
            <cite><span id="out_url" ><?php echo $site ?></span></cite>
        </div>
        <div>
            <span id="out_snippet">sdsdsdfvbfbf</span>
        </div>
    </div>

答案是:为什么只有第一个方法被成功调用,而其他两个方法生成错误?

您的第一个函数似乎缺少一个闭合的}括号。最后一个括号来自else块。固定:

function textCounter(id_Tf, id_Cd) {
    // Method that is called succesfully
    var element = document.getElementById(id_Tf);   
    var nameLenght  = element.value.length;    
    var countDisplay = document.getElementById(id_Cd);  
    if(nameLenght <= maxAmount) {
        countDisplay.value = maxAmount - nameLenght;
    }
    else {
        countDisplay.value = "0";
    }
}

首先用</script> 关闭js.php文件末尾的<script>阶段

不要在<doctype>标记之前包含js文件。

</body> 之前将其包含在<head>标记OR中