2016-07-25 205 views
0

我一直有一個問題,調用一個外部.js文件中聲明的函數。 我已經在主頁面中包含了.js文件,但顯然它部分起作用(只是其中一個函數正確調用,其他函數生成「Uncaught ReferenceError:函數未定義」)未捕獲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> 

答案是:爲什麼只是第一甲基od被稱爲成功,而另外兩個產生錯誤?

回答

2

您的第一個功能似乎缺少關閉}括號。最後一個支架來自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"; 
    } 
} 
+1

順便說一句,那些一種「錯誤」的,可以很容易地與任何良好的代碼編輯器檢測到的(視覺代碼,凌動,甚至記事本+ +),因爲它突出了大括號的時候盤旋開幕之一! –

0

首先,在您的js.php文件末尾的所有密切<script>塔格BYV </script>

<doctype>標籤不包括的js文件。

包括在<head>標籤或之前</body>

相關問題