使用window.open()时,$(document).ready不会在打开的窗口中触发

时间:2016-05-24 16:41:21

标签: javascript jquery asp.net-mvc

我打开一个新窗口,在MVC 4应用程序中使用javascript和jquery显示报告。代码如下。

window.open方法:

    $(document).ready(function () {
        // validation stuff
            submitHandler: function (form) {
                var brewery = document.getElementById('BrewerySelect').value;
                var line = document.getElementById('LineSelect').value;
                var day = document.getElementById('datepicker').value;
                var width = window.innerWidth * 0.66;
                var height = width * window.innerHeight / window.innerWidth;
                var urlStr = '/DashboardReport/Report?brewery=' + brewery + '&line=' + line.trim() + '&day=' + day;
                alert(urlStr);
                window.open(urlStr, 'newwindow', 'width=' + width + ', height=' + height + ', top=' + ((window.innerHeight - height) / 2) + ', left=' + ((window.innerWidth - width) / 2));
            }
        });
    });

控制器什么都不做,我已经尝试过PartialViewResult和ActionResult,控制器中的其余方法适用于ajax调用。该报告以模式工作:

    public ActionResult Report()
    {
        return View();
    }

打开的页面:

@{
    Layout = null;
}
<html>
<head>
    <title>Report</title>
    <link href="~/Content/bootstrap.min.css" rel="stylesheet" type="text/css" />
    <link href="~/Content/Site.css" rel="stylesheet" type="text/css" />

</head>
<body>
    <div id="reportBody" style="height: 100%;">

    </div>

    <script src="~/Scripts/jquery-1.7.1.min.js" type="text/javascript"></script>
    <script src="~/Scripts/scripts.js" type="text/javascript"></script>
    <script type="text/javascript">        
        $(document).ready(function () {
            var brewery = GetURLParameter('brewery');
            var line = GetURLParameter('line');
            var day = GetURLParameter('day');
            alert('document hit');
            SetReport(brewery, line, day);
        });

        function GetURLParameter(sParam)
        {
            var sPageURL = window.location.search.substring(1);
            var sURLVariables = sPageURL.split('&');
            for (var i = 0; i < sURLVariables.length; i++)
            {
                var sParameterName = sURLVariables[i].split('=');
                if (sParameterName[0] == sParam)
                {
                    return sParameterName[1];
                }
            }
        }​

        function SetReport(brewery, line, day) {
            var url = '@Url.Action("GetUrl")';
            alert('SetReport Hit ( action url = ' + url + ')');
            $.ajax({
                url: url,
                data: { breweryCode: brewery, packageLine: line, date: day },
                dataType: 'json',
                cache: false,
                type: "POST",
                success: function (data) {
                    alert('SetReport success. data = ' + data);
                    var url = '<iframe src="' + data + '" height="100%" width="100%" scrolling="auto"></iframe>';                           
                    $('#reportBody').html(url).show();                           
                },
                error: function (response) {
                    alert('document.ready() dashboardReportForm SetForm() method failed');
                }
            });
        }
    </script>
</body>
</html>

我已经在整个javascript中设置了警报,让我知道被点击的内容,但没有任何警报被触发。 document.ready函数没有被击中。

1 个答案:

答案 0 :(得分:1)

GetURLParameter函数的结束括号后面有一个U + 200b字符,这会导致语法错误。删除它,它应该工作。

请参阅No visible cause for "Unexpected token ILLEGAL"