2015-05-29 62 views
0

我有一個母版頁,其中包括以下腳本:在母版頁身體的onload調用jQuery函數:0x800a1391 - JavaScript的運行時錯誤:函數是未定義

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
 
<html xmlns="http://www.w3.org/1999/xhtml"> 
 
<head runat="server"> 
 
\t <title><asp:ContentPlaceHolder ID="TitleContent" runat="server" /></title> 
 
\t 
 
\t <script src="<%=Url.Content("~/Scripts/jquery-1.4.4.min.js") %>" type="text/javascript"></script>  
 
\t <script src="<%=Url.Content("~/Scripts/jquery-ui-1.8.7.custom.min.js") %>" type="text/javascript"></script> 
 

 
\t <link href="<%=Url.Content("~/Content/Site.css") %>" type="text/css" rel="Stylesheet" /> 
 
\t <link href="<%=Url.Content("~/Content/jquery-ui/pepper-grinder/jquery-ui-1.8.7.custom.css") %>" type="text/css" rel="Stylesheet" /> 
 

 
\t <script src="<%=Url.Content("~/Scripts/MicrosoftAjax.js") %>" type="text/javascript"></script> 
 
\t <script src="<%=Url.Content("~/Scripts/MicrosoftMvcAjax.js") %>" type="text/javascript"></script> 
 
\t <script src="<%=Url.Content("~/Scripts/MicrosoftMvcValidation.js") %>" type="text/javascript"></script> 
 

 
\t <% if (false) { %> 
 
\t \t \t <script src="<%=Url.Content("~/Scripts/jquery-1.4.1-vsdoc.js") %>" type="text/javascript"></script>  
 
\t <% } %> 
 

 

 
\t <script type="text/javascript"> 
 
\t  $(document).ready(function() { 
 

 
\t   $(function hideMessage() { 
 
\t    $('.sessionMessage').delay(5000).fadeOut(2000); 
 
\t   }) 
 

 

 
\t \t \t $('[id $=Link]').click(function() { 
 
\t \t \t \t var txt = $(this).attr("id").substr(0, $(this).attr("id").length - 4); 
 
\t \t \t \t $.ajax({ 
 
\t \t \t \t \t url: this.href, 
 
\t \t \t \t \t cache: false, 
 
\t \t \t \t \t success: function (result) { 
 
\t \t \t \t \t \t $('[id ^=' + txt + '][id $=Table]').append($(result)); 
 
\t \t \t \t \t }, 
 
\t \t \t \t \t error: function (xhr, err) { 
 
\t \t \t \t \t \t alert("readyState: " + xhr.readyState + "\nstatus: " + xhr.status + "\nresponseText: " + xhr.responseText); 
 
\t \t \t \t \t } 
 
\t \t \t \t }); 
 
\t \t \t \t return false; 
 
\t \t \t }); 
 
\t \t }); 
 
\t </script>

在同一個母版頁,對身體標記,我調用了hideMessage功能:

<body onload="hideMessage();">

當我調試應用程序,我得到以下信息:

0x800a1391 - JavaScript runtime error: 'hideMessage' is undefined

我一直在試圖腳本位置並沒有什麼的每一個排列似乎影響這個小小的腳本的有效性。所有其他腳本似乎加載正常,並且應用程序運行(至少在初始階段)。

+0

嘗試調用'hideMessage()'直接在'document.ready()' –

+0

@Sindersindersh你的意思是在聲明之後調用函數,在body標籤之外?編輯:我試過了,結果相同。非常奇怪...... – morganpdx

+0

如果你想打電話像

回答

0

如果你打電話給你之類的函數<body onload='fn_name()'話外document.ready()

<head> 
    <script> 
     function hideMessage(){ 
     alert("Hello world"); 
     } 
    </script> 
    </head> 
    <body onload="hideMessage();"> 
    hello world ... 
    </body> 

OR

創建而是調用功能齊全<body onload='fn_name()'直接調用正如我在我的第一則留言中提到你的FUNC .IE

<script> 
    $(document).ready(function(){ 
     helloWorld(); // when dom is ready this function gets call 

     function helloWorld(){ 
      alert("helloWorld");   
     } 
}); 

</script> 
<body> 
hello world 

</body> 
+0

我標記了你的答案,因爲它讓我走上了正確的軌道。 ..我不得不從函數調用中刪除'$'。不知道爲什麼它在那裏擺在首位。 – morganpdx

+0

@morganpdx:很高興爲您效勞,快樂編碼:-) –

0
You can defined the function outside the $(document).ready function and then call it whenever you need 

<script type="text/javascript"> 

function hideMessage(){ 
$('.sessionMessage').delay(5000).fadeOut(2000); 
} 
     $(document).ready(function() { 

      hideMessage(); 


      $('[id $=Link]').click(function() { 
       var txt = $(this).attr("id").substr(0, $(this).attr("id").length - 4); 
       $.ajax({ 
        url: this.href, 
        cache: false, 
        success: function (result) { 
         $('[id ^=' + txt + '][id $=Table]').append($(result)); 
        }, 
        error: function (xhr, err) { 
         alert("readyState: " + xhr.readyState + "\nstatus: " + xhr.status + "\nresponseText: " + xhr.responseText); 
        } 
       }); 
       return false; 
      }); 
     }); 
    </script> 
相關問題