2016-07-25 202 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>

相关问题