在document.write之后JavaScript不起作用

时间:2012-05-22 12:41:44

标签: javascript jquery ajax document.write

我正在使用jQuery BlockUI打开一个新网页,由于大量数据库使用以下javascript查询需要一些时间:

function productSheet(url2) {
       $.blockUI.defaults.overlayCSS = {};
        $.blockUI({ });
        $.ajax({
            url: url2,
            success: function (respones) {
                var win = window.open();
                with (win.document) {
                    open();
                    write(respones);
                    close();
                }
            }
        });
    };

在新页面我得到了一些jQuery JavaScript和对外部jQuery脚本的引用。但是,当我在上面的JavaScript之后呈现页面时,我的所有脚本都会抛出错误:“$ undefined”。 我可以刷新页面,一切都开始工作,我没有收到任何脚本错误。

这个问题只发生在我在IE 9中调试时,在Firefox上一切正常(没有JavaScript错误和脚本工作)。

有没有人知道问题是什么?

编辑:

页面iam渲染是MVC 3视图。所以上面的脚本转到MVC动作,返回一个这个视图:

@model WebApplication.Controllers.ProductSheetModel
<!DOCTYPE html>
<html>
<head>

<title>Sheet - @Model.ArticleMain.ArticleMain.T0018_BENAM</title>

    <script src="../../js/jquery-1.3.2-vsdoc2.js" type="text/javascript"></script>
    <link href="../../css/ProductSheet.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="wrapper">
    @if (Model.IsPDFExport == false)
    { 
        @Html.DisplayFor(model => model.ArticleMain, "ProductSheetHeader")
    }
    ... some more partical views...
</div>
</body>
</html>
<script type="text/javascript">
$(document).ready(function () {
    var tabelheight1 = $("#divNutritiveValues").height();
    var tabelheight2 = $("#divMarking").height();
    if (tabelheight1 > tabelheight2) {
        $("#divMarking").css("height", tabelheight1 + "px");
        $("#divNutritiveValues").css("height", tabelheight1 + "px");
    }
    if (tabelheight2 > tabelheight1) {
        $("#divNutritiveValues").css("height", tabelheight2 + "px");
        $("#divMarking").css("height", tabelheight2 + "px");
    }

    var tableheightStore = $("#divStore").height();
    var tableheightCooking = $("#divCooking").height();
    if (tableheightCooking > tableheightStore) {
        $("#divCooking").css("height", tableheightCooking + "px");
        $("#divStore").css("height", tableheightCooking + "px");
    }
    if (tableheightStore > tableheightCooking) {
        $("#divCooking").css("height", tableheightStore + "px");
        $("#divStore").css("height", tableheightStore + "px");
    }

    var tableInfoProvid = $("#divInformationProvider").height();
    var tableManufac = $("#divManufacturer").height()
    if (tableInfoProvid > tableManufac) {
        $("#divManufacturer").css("height", tableInfoProvid + "px");
        $("#divInformationProvider").css("height", tableInfoProvid + "px");
    }
    if (tableManufac > tableInfoProvid) {
        $("#divInformationProvider").css("height", tableManufac + "px");
        $("#divManufacturer").css("height", tableManufac + "px");
    }
});

3 个答案:

答案 0 :(得分:0)

问题是在IE页面上未定义$ $。

通常,jQuery将$设置为jQuery对象的引用。 See docs

出于某种原因,要么删除$引用,要么在IE上没有发生。你能发布更多的代码吗?

一种解决方法是使用jQuery代替$

但是,更好地了解IE上实际发生的事情会很好。

答案 1 :(得分:0)

我认为这是因为您的AJAX帖子没有发生JavaScript注册,这意味着它们会丢失并且不会重新注册。

使用AJAX时这是一个常见问题,this article完美地解释了它并提供了您所需要的内容。

答案 2 :(得分:0)

我提出了一个解决方案,并希望与您分享。在我的网站中,我创建了一个脚本,检查jquery是否未定义。

if (typeof jQuery == 'undefined') {
      window.location.reload();
 }

我再次重新加载页面后,我的所有脚本都工作,也许不是最好的解决方案,但它确实有效,所以我很高兴。

相关问题