简体   繁体   English

IE8 DOM无法反映JS更改

[英]IE8 DOM does not reflect JS changes

I'm making some simple changes using javascript to HTML elements already existant when the page is served (such as changing background images of div elements, adding IDs etc). 我正在使用javascript对页面提供时已经存在的HTML元素进行一些简单的更改(例如更改div元素的背景图片,添加ID等)。 This of course works fine in every browser apart from IE8 where the change doesn't appear to be reflected in the DOM so when I parse the dom after the JS has run it cant find the elements I'm looking for. 当然,这在除IE8之外的所有浏览器中都可以正常工作,因为IE8中的更改似乎未反映在DOM中,因此当我在JS运行后解析dom时,它找不到我想要的元素。 The page is built up of 2 javascript files in the header, 1 is an external third party script which I do not have control over but which is the one adding the ids and background images. 该页面在标题中包含2个javascript文件,其中1个是我无法控制的外部第三方脚本,但该脚本添加了ID和背景图片。 The second is mine which is called after the first and is parsing the document looking for the specific elements with the new IDs. 第二个是我的,它在第一个之后被调用,并且正在解析文档以查找具有新ID的特定元素。 Both are external scripts and are not inline in the HTML source. 两者都是外部脚本,并且不在HTML源代码中内联。

From what I can tell its either: 从我可以说的是:

  • a race condition, 2 external Javascripts are running 1 is changing the buttons and adding the ids and the other is parsing the dom looking for specific elements and as they're running at the same time the second never finds the elements 一种竞争条件,正在运行2个外部Javascript 1正在更改按钮并添加ID,另一个正在解析dom以查找特定元素,而当它们同时运行时,第二个Javascript找不到元素
  • IE8 does not properly refresh the DOM after changes have been made 进行更改后,IE8无法正确刷新DOM

My JS is called after the first JS in the head so you would assume that the blocking would not cause the race condition and the elements would be available before my JS runs 我的JS在头部的第一个JS之后被调用,因此您可以认为阻塞不会导致竞争条件,并且在我的JS运行之前这些元素将可用

Things I've tried: 我尝试过的事情:

  • I've tried adding a class to the body to force a refresh of the DOM before my code runs 我尝试在主体上添加一个类以强制刷新DOM,然后再运行代码
  • I've used IE8 developer tools and the ids and elements are not present, but if I refresh a few times they magically appear (the page has already fully loaded at the this point and I can interact with it fully) 我使用了IE8开发人员工具,但id和元素不存在,但是如果我刷新几次,它们就会神奇地出现(此时页面已经完全加载,可以完全与之交互)

Any ideas? 有任何想法吗?

Thanks! 谢谢!

One thing to keep in mind is that when you are creating multiple <script> tags, you are not guaranteed of the order in which they load, and depending on how they are build - they will generally begin processing as soon as they are loaded. 要记住的一件事是,当您创建多个<script>标记时,不能保证它们的加载顺序以及它们的构建方式-它们通常会在加载后立即开始处理。

So, if you are including one local file and one file to a CDN, such as: 因此,如果要在CDN中包含一个本地文件和一个文件,例如:

<script type="text/javascript" 
        src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js">
</script>
<script type="text/javascript" src="/js/my_script.js"></script>

You have to take into account that the CDN file is often far faster for delivery than your hosted file. 您必须考虑到CDN文件通常比托管文件要快得多。 In the above case, this may be a good thing - because jQuery being loaded on the page before your script is probably ideal - but if you are loading a different 3rd party script, which may rely on certain elements being present in the DOM that your script is responsible for creating, your script may not create them in time. 在上述情况下,这可能是一件好事-因为jQuery网页上加载之前,你的脚本可能是理想的-但如果你是加载不同的第三方脚本,这可能依赖于某些元素存在于DOM, 脚本负责创建,您的脚本可能未及时创建它们。

Imagine this scenario: 想象一下这种情况:

<script type="text/javascript" src="https://someurl/somelib.js">
    // This script parses the DOM and applies alterations to certain items
</script>
<script type="text/javascript" src="/js/my_script.js">
    // This script creates the DOM elements the other script is supposed to alter
</script>

Your page will only work if the local file, /js/my_script.js , loads first - which is unlikely because the other file is being served from a dedicated CDN. 仅当本地文件/js/my_script.js首先加载时,您的页面才能工作-这不太可能,因为另一个文件是通过专用CDN提供的。

This is even worse when both files are served locally, such as: 当两个文件都在本地提供时,情况甚至更糟:

<script type="text/javascript" src="/js/my_relied_upon_script.js"></script>
<script type="text/javascript" src="/js/my_reliant_script.js"></script>

In this case, it all depends on how your local web server happens to handle the HTTP requests in order to determine what happens in what order. 在这种情况下,这完全取决于您的本地Web服务器如何处理HTTP请求,以便确定以什么顺序发生了什么。

So - on to the solution: 因此-解决方案:

1) Make your scripts all wait for the document 's onready event to fire. 1)让脚本全部等待documentonready事件触发。 Because this event only occurs once the document is fully loaded (including any other HTTP requests necessary to fully load its elements, such as scripts, images, etc.) - you can be guaranteed that the scripts will at least wait until the full DOM is loaded. 因为此事件仅在文档完全加载后才发生(包括完全加载其元素(例如脚本,图像等)所需的任何其他HTTP请求),因此可以确保脚本至少要等到完整的DOM被加载后,已加载。

2) Make subordinate scripts wait for trigger events. 2)使下级脚本等待触发事件。

With jQuery, an example might be something like the following: 使用jQuery,示例可能类似于以下内容:

// Script #1
$(document).bind('ready', function () {
    $('#NeedsBackground').css({ background: 'url(/gfx/bg.png)' });

    var $wrapper = $('<div />').addClass('wrapper');
    $('#NeedsWrapper').wrap($wrapper);

    // Here's the magic that enforces loading.
    $(document).trigger('Script1Finished');
});

// Script #2
$(document).bind('Script1Finished', function () {
    $('.wrapper').css({ border: '1px solid #000' });
});

Now - bear in mind that the above transformations are fairly terrible, and not something you'd want to do (such as inlining CSS and such, generally) - but they give an example. 现在-请记住,上面的转换相当糟糕,不是您想要执行的操作(例如,内联CSS等),但是它们仅举了一个例子。 Because Script #2 requires that the .wrapper elements exist before running, you need to ensure that it happens AFTER Script #1 occurs. 因为Script #2要求.wrapper元素在运行之前存在,所以您需要确保它在Script #1发生后发生。

In this case, we're accomplishing that by trigger ing a custom event on the document, which we can then respond to - and we are only firing that event after the DOM has been put in the proper state. 在这种情况下,我们通过在文档上trigger一个自定义事件来实现此目的,然后我们可以响应该事件,并且仅在DOM处于适当状态后才触发该事件。

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

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