JavaScript 在 document.write 之后不起作用

JavaScript doesn't work after document.write

本文关键字:之后 不起作用 write document JavaScript      更新时间:2023-09-26

我正在使用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");
    }
});

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

通常,jQuery将$设置为对jQuery对象的引用。请参阅文档。

由于某种原因,$ 引用正在被删除或未在 IE 上发生。你能发布更多的代码吗?

一种解决方法是使用jQuery而不是$

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

我相信

这是由于您的 JavaScript 注册没有发生在您的 AJAX 帖子中,这意味着它们会丢失并且不会重新注册。

这是使用 AJAX 时的常见问题,本文对此进行了完美的解释,并提供了您所需要的内容。

我想

出了一个解决方案,想与你分享。在我的网站上,我创建了一个脚本,检查jquery是否未定义。

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

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