简体   繁体   English

在document.write之后JavaScript无法使用

[英]JavaScript doesn't work after document.write

I am using jQuery BlockUI for opening a new webpage that takes some time due to a lot of database querying with the following javascript: 我正在使用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();
                }
            }
        });
    };

At the new page i got some jQuery JavaScript and a reference to the external jQuery script. 在新页面上,我获得了一些jQuery JavaScript和对外部jQuery脚本的引用。 However when i render the page after the JavaScript above all my script throws error for: "$ undefined". 但是,当我在所有JavaScript上方的JavaScript之后呈现页面时,我的脚本将引发错误:“ $ undefined”。 I can refresh the page and everything starts working and I am not getting any script errors. 我可以刷新页面,所有内容开始工作,并且没有任何脚本错误。

This problem only occur when I am debugging in IE 9, on Firefox everything works (no JavaScript errors and the script works). 仅当我在IE 9上进行调试时,才会出现此问题。

Does anyone have any idea on whats the problem can be? 有人对问题可能有什么想法吗?

EDIT: 编辑:

The page iam rendering is a MVC 3 view. 页面IAM渲染是MVC 3视图。 So the script above goes to an MVC action that returns a this view: 因此,上面的脚本转到一个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");
    }
});

The problem is that $ is undefined on your page on IE. 问题是$在IE的页面上是未定义的。

Normally, jQuery sets $ as a reference to the jQuery object. 通常,jQuery将$设置为对jQuery对象的引用。 See docs . 参见docs

For some reason, either the $ reference is being removed or is not happening on IE. 由于某种原因,$引用被删除或在IE上没有发生。 Can you post more of your code? 您可以发布更多代码吗?

One work-around would be to use jQuery instead of $ 一种解决方法是使用jQuery而不是$

But it would be good to better understand what is actually happening on IE. 但是最好能更好地了解IE上实际发生的情况。

I believe this is caused as your JavaScript registrations are not occurring with your AJAX posts, meaning that they get lost and don't get re-registered. 我相信这是由于您的AJAX帖子中没有发生JavaScript注册而引起的,这意味着它们会丢失并且不会被重新注册。

This is a common issue when working with AJAX, and this article explains it perfectly and provides what you need. 这是使用AJAX时的常见问题, 本文将对其进行完美地解释并提供您所需的内容。

i came up with a solution and want to share it with you. 我想出了一个解决方案,想与您分享。 In my site i created a script checking if jquery was undefined. 在我的网站上,我创建了一个脚本来检查jquery是否未定义。

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

after i have reloaded the page again all my scripts work, maybe not the best of solutions but it works so i am happy. 在我重新加载页面后,我所有的脚本都能正常工作,也许不是最好的解决方案,但它可以工作,所以我很高兴。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM