简体   繁体   English

将JavaScript放在单独的JS文件中后,ASP.net MVC 3中的JScript运行时错误

[英]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.

相关问题 JScript运行时错误:AjaxControlToolkit需要ASP.NET 4.0 - JScript Runtime Error: AjaxControlToolkit requires ASP.NET 4.0 数据表行重新排序期间的JavaScript运行时错误(ASP.Net MVC) - JavaScript runtime error during DataTables rowReordering (ASP.Net MVC) Microsoft JScript运行时错误:AjaxControlToolkit需要ASP.NET Ajax 4.0脚本 - Microsoft JScript runtime error: AjaxControlToolkit requires ASP.NET Ajax 4.0 scripts ASP.NET Javascript 运行时错误 - ASP.NET Javascript runtime error asp.net MVC 4 0x80070005 - Javascript运行时错误:访问被拒绝 - asp.net MVC 4 0x80070005 - Javascript runtime error: access denied 异常0x800a1391-JavaScript运行时错误:在ASP.NET MVC中未定义&#39;JSON&#39; - Exception 0x800a1391 - JavaScript runtime error: 'JSON' is undefined in asp.net mvc 在ASP.Net MVC中在运行时切换appSettings文件路径 - Switching appSettings file path at runtime in ASP.Net MVC 在ASP.NET MVC中下载文件时出错 - Error in downloading a file in ASP.NET MVC SliderExtender导致0x800a139e-Microsoft JScript运行时错误:AjaxControlToolkit是否需要ASP.NET Ajax 4.0脚本? - SliderExtender causing 0x800a139e - Microsoft JScript runtime error: AjaxControlToolkit requires ASP.NET Ajax 4.0 scripts? ASP.NET MVC在隐藏字段中放置用户ID - ASP.NET MVC placing userID in hidden field
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM