簡體   English   中英

在document.write之后JavaScript無法使用

[英]JavaScript doesn't work after 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上方的JavaScript之后呈現頁面時,我的腳本將引發錯誤:“ $ undefined”。 我可以刷新頁面,所有內容開始工作,並且沒有任何腳本錯誤。

僅當我在IE 9上進行調試時,才會出現此問題。

有人對問題可能有什么想法嗎?

編輯:

頁面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對象的引用。 參見docs

由於某種原因,$引用被刪除或在IE上沒有發生。 您可以發布更多代碼嗎?

一種解決方法是使用jQuery而不是$

但是最好能更好地了解IE上實際發生的情況。

我相信這是由於您的AJAX帖子中沒有發生JavaScript注冊而引起的,這意味着它們會丟失並且不會被重新注冊。

這是使用AJAX時的常見問題, 本文將對其進行完美地解釋並提供您所需的內容。

我想出了一個解決方案,想與您分享。 在我的網站上,我創建了一個腳本來檢查jquery是否未定義。

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

在我重新加載頁面后,我所有的腳本都能正常工作,也許不是最好的解決方案,但它可以工作,所以我很高興。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM