【问题标题】:Not able to call js file function无法调用js文件函数
【发布时间】:2014-12-30 05:55:36
【问题描述】:

我正在使用一个 js 文件,它会在用户会话即将结束之前给我一个弹出窗口。我以两种方式使用这个文件

1) 在 Web 表单应用程序中 - 它工作得很好。以下是它的代码。 我正在关注this链接

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">



<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Session Time Out Warning Message</title>

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.0/jquery.min.js"></script>

    <script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.12/jquery-ui.min.js"></script>

    <script src="Script/timeout-dialog.js" type="text/javascript"></script>

    <link href="css/timeout-dialog.css" rel="stylesheet" type="text/css" />

    <script type="text/javascript" language="javascript">

        function Timer(time) {

            setTimeout(TimeOut, time);
        }

        function TimeOut() {
            alert(window.location.pathname);
            $.timeoutDialog({
                timeout: 0.15,
                countdown: 60,
                keep_alive_url: window.location.pathname,
                logout_redirect_url: '/AutoSessionTimeOut/SessionTime.aspx',
                restart_on_yes: true
            });
        }

    </script>

</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label ID="lblMsg" runat="server" Text=""></asp:Label>
    </div>
    </form>
</body>
</html>

cs代码:

if (!IsPostBack)
        {
            int _displayTimeInMiliSec = (Session.Timeout - 1) * 60000;

            if (Session["ID"] == null)
            {
                Session["ID"] = "New Session";
                lblMsg.Text = Convert.ToString(Session["ID"]);
            }
            else
                lblMsg.Text = "Old Session";

            ScriptManager.RegisterClientScriptBlock(this.Page, this.Page.GetType(),
                "message",
                "<script type=\"text/javascript\" language=\"javascript\">Timer('" + _displayTimeInMiliSec + "');</script>",
                false);
        }

2) 我在 MVC 应用程序中使用它。

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>@ViewBag.Title - My ASP.NET Application</title>
    @Styles.Render("~/Content/css")
    @Scripts.Render("~/bundles/modernizr")
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.js"></script>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1/jquery-ui.js"></script>

    <script src="Scripts/timeout-dialog.js"></script>
    <link href="Content/timeout-dialog.css" rel="stylesheet" />
    <script type="text/javascript">

        function Timer(time) {

            setTimeout(TimeOut, time);
        }

        function TimeOut() {
            //window.location.pathname = '/Home/Index';
            alert(window.location.pathname);
            alert('hi');
            $.timeoutDialog({
                timeout: 0.25,
                countdown: 30,
                keep_alive_url: window.location.pathname,
                logout_redirect_url: '/Home/Index',
                restart_on_yes: true
            });

        }


    </script>



</head>
<body onload="Timer(15000)">

    <div class="navbar navbar-inverse navbar-fixed-top">
        <div class="container">
            <div class="navbar-header">
                <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>
                @Html.ActionLink("Application name", "Index", "Home", new { area = "" }, new { @class = "navbar-brand" })
            </div>
            <div class="navbar-collapse collapse">
                <ul class="nav navbar-nav">
                    <li>@Html.ActionLink("Home", "Index", "Home")</li>
                    <li>@Html.ActionLink("About", "About", "Home")</li>
                    <li>@Html.ActionLink("Contact", "Contact", "Home")</li>
                </ul>
                @Html.Partial("_LoginPartial")
            </div>
        </div>
    </div>
    <div class="container body-content">
        @RenderBody()
        <hr />
        <footer>
            <p>&copy; @DateTime.Now.Year - My ASP.NET Application</p>
        </footer>
    </div>

    @Scripts.Render("~/bundles/jquery")
    @Scripts.Render("~/bundles/bootstrap")
    @RenderSection("scripts", required: false)
</body>
</html>

在这里,当我在上面提到的 $.timeoutDialog({ 行之前发出警报时,警报会出现,但其余代码未执行,并且我的网络表单应用程序中没有任何弹出窗口。

我在MVC 应用程序中做错了什么。请帮忙。

【问题讨论】:

  • 所有 JS 文件都加载了吗?
  • @Shil :我如何检查。??
  • 请使用在telerik.com/fiddler找到的类似提琴手的工具
  • 如果您使用的是 Chrome 浏览器,请使用控制台调试代码
  • 在chrome或IE中按F12可以调试

标签: javascript asp.net-mvc session-timeout


【解决方案1】:

我认为timeout参数应该是here提到的整数

【讨论】:

  • 我只是不明白,相同的代码在 web 表单中和链接中可以正常工作,但在 mvc 中不起作用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多