[英]JScript Runtime Error in ASP.net MVC 3 after placing the javascript in a separate JS File
Hi I've got this Javascript Runtime Error in ASP.net MVC3. 嗨,我在ASP.net MVC3中遇到了Javascript Runtime Error。 The code fragment below works just fine but the problem here goes when I try to place the displayTime() into another .js file.... I tried placing the whole javascript function in a separate js file then adding another import like these:
下面的代码片段工作正常,但是当我尝试将displayTime()放入另一个.js文件时,这里的问题就解决了。...我试图将整个javascript函数放在一个单独的js文件中,然后添加如下所示的另一个导入:
<script src="@Url.Content("~/Scripts/customFunction.js")" type="text/javascript"></script>
right after the script Ive imported... all of this codes are placed in my _Layout.cshtml file. Ive脚本导入后,...所有这些代码都放在我的_Layout.cshtml文件中。 But unfortunate a MS JScript Runtime Error Occured in the
<body onload="displayTime()">
saying Object Expected.... :( Hope someone can help 但是不幸的是,在
<body onload="displayTime()">
发生了MS JScript运行时错误,提示<body onload="displayTime()">
期望对象<body onload="displayTime()">
<body onload="displayTime()">
<!-- scripts -->
<script src="@Url.Content("~/Scripts/jquery-1.7.2.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery-ui-1.8.20.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")" type="text/javascript"></script>
<script type="text/javascript">
function displayTime()
{
}
</script>
</body>
Here is the full code of the JS Function: 这是JS函数的完整代码:
var serverTime = @DateTime.Now.TimeOfDay.TotalSeconds;
var serverOffset = serverTime - getClientTime();
function getClientTime()
{
var time = new Date();
return (time.getHours() * 60 * 60) +
(time.getMinutes() * 60) + (time.getSeconds());
}
function displayTime()
{
var serverTime = getClientTime() + serverOffset;
var hours = Math.floor(serverTime / 60 / 60).toString();
var minutes = Math.floor(serverTime / 60 % (hours * 60)).toString();
var seconds = Math.floor(serverTime % 60).toString();
hours = hours.length == 1 ? '0'+ hours : hours;
minutes = minutes.length == 1 ? '0'+ minutes : minutes;
seconds = seconds.length == 1 ? '0'+ seconds : seconds;
document.getElementById("clock").innerHTML = hours + ":" +
minutes + ":" + seconds; // <-- updates the "clock" div.
setTimeout(displayTime, 1000); // <-- calls this function again in 1 second.
}
<head>
<script type="text/javascript">
var serverTime = @DateTime.Now.TimeOfDay.TotalSeconds;
</script>
<script src="@Url.Content("~/Scripts/customFunction.js")" type="text/javascript"></script>
</head>
<body onload="displayTime()">
</body>
The script containing this displayTime
function should be placed in your <head>
section. 包含此
displayTime
函数的脚本应放在您的<head>
部分中。 Alternatively since you are using jQuery, why don't you simply use the $(document).ready
function? 另外,由于您使用的是jQuery,为什么不简单地使用
$(document).ready
函数呢?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.