在Master Page body onload上调用jQuery函数:0x800a1391 - JavaScript运行时错误:函数未定义

时间:2015-05-29 18:43:13

标签: javascript c# jquery html asp.net-mvc-4

我有一个母版页,其中包含以下脚本:

<!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">
	<title><asp:ContentPlaceHolder ID="TitleContent" runat="server" /></title>
	
	<script src="<%=Url.Content("~/Scripts/jquery-1.4.4.min.js") %>" type="text/javascript"></script>    
	<script src="<%=Url.Content("~/Scripts/jquery-ui-1.8.7.custom.min.js") %>" type="text/javascript"></script>  

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

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

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


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

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


			$('[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>

在同一母版页的body标签上,我调用了hideMessage函数:

<body onload="hideMessage();">

当我调试应用程序时,我收到以下消息:

  

0x800a1391 - JavaScript运行时错误:'hideMessage'未定义

我一直在尝试脚本位置的每个排列,似乎没有任何东西影响这个小小脚本的有效性。所有其他脚本似乎都正常加载,应用程序运行(至少最初)。

2 个答案:

答案 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()'直接调用.i.e

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

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

</script>
<body>
hello world

</body>

答案 1 :(得分: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>