javascript 传奇_JS包括–传奇继续……

news/2024/7/19 12:58:23 标签: js, javascript, css, php, html
htmledit_views">
<a class=html" title=javascript>javascript 传奇" width="403px" height="256px" style="outline: none;" />

html" title=javascript>javascript 传奇

The problem in question is how to find out a dynamically included JavaScript file is actually loaded. The concept of JavaScript includes is here, the IE-only solution is here. The IE solution is to use the onreadystatechange event that is fired when a new script is included. It also works for dynamically loaded CSS files using a new link DOM element. Thanks to the comment from Björn Graf, I tried using onload event to test if the new script is included using Firefox. It worked!

有问题的问题是如何找出实际包含的动态包含JavaScript文件。 JavaScript include的概念在这里,仅IE的解决方案在这里。 IE解决方案是使用包含新脚本时触发的onreadystatechange事件。 它也适用于使用新的link DOM元素动态加载CSS文件。 感谢BjörnGraf的评论,我尝试使用onload事件来测试新脚本是否包含在Firefox中。 有效!

代码 (The code)

What we have here (demo) is trying to include a .html" title=js>js file and an .html" title=css>css file, creating new script and link DOM elements. Then I'm attaching event listeners to those new elements - one onload and one onreadystatechange. The script that is included (html" title=js>jsalert.html" title=js>js) has one alert().

我们在这里(演示)正在尝试包括.html" title=js>js文件和.html" title=css>css文件,创建新scriptlink DOM元素。 然后,我将事件侦听器附加到这些新元素上-一个onload和一个onreadystatechange 。 包含的脚本( html" title=js>jsalert.html" title=js>js )具有一个alert()

var html" title=css>css;
function include_html" title=css>css(html" title=css>css_file) {
    var html_doc = document.getElementsByTagName('head')[0];
    html" title=css>css = document.createElement('link');
    html" title=css>css.setAttribute('rel', 'stylesheet');
    html" title=css>css.setAttribute('type', 'text/html" title=css>css');
    html" title=css>css.setAttribute('href', html" title=css>css_file);
    html_doc.appendChild(html" title=css>css);
 
    // alert state change
    html" title=css>css.onreadystatechange = function () {
        if (html" title=css>css.readyState == 'complete') {
            alert('CSS onreadystatechange fired');
        }
    }
    html" title=css>css.onload = function () {
        alert('CSS onload fired');
    }
    return false;
}
 
 
var html" title=js>js;
function include_html" title=js>js(file) {
    var html_doc = document.getElementsByTagName('head')[0];
    html" title=js>js = document.createElement('script');
    html" title=js>js.setAttribute('type', 'text/html" title=javascript>javascript');
    html" title=js>js.setAttribute('src', file);
    html_doc.appendChild(html" title=js>js);
 
    html" title=js>js.onreadystatechange = function () {
        if (html" title=js>js.readyState == 'complete') {
            alert('JS onreadystate fired');
        }
    }
 
    html" title=js>js.onload = function () {
        alert('JS onload fired');
    }
    return false;
}

结果 (Results)

As you can probably guess, the results are different in IE and FF.

您可能会猜到,IE和FF的结果是不同的。

  • CSS inclusion - IE fires both events, onload first, then onreadystatechange. FF fires nothing.

    CSS包含-IE会触发两个事件,首先触发onload ,然后触发onreadystatechange 。 FF不会发射任何东西。

  • JS inclusion - IE fires onreadystatechange. FF fires onload. Both will execute the script before firing the event.

    JS包含-IE会触发onreadystatechange 。 FF火灾onload 。 两者都将触发事件之前执行脚本。

结论 (Conclusion)

1. So there is, after all, a cross-browser way to tell when a JavaScript is actually included and that is to attach two event listeners - onload and onreadystatechange. 2. In IE you have two ways to tell when a CSS is included.

1.毕竟,有一种跨浏览器的方式可以告诉您何时实际包含JavaScript,并且要附加两个事件侦听器: onloadonreadystatechange 。 2.在IE中,您可以通过两种方法判断何时包括CSS。

Tell your friends about this post on Facebook and Twitter

在Facebook和Twitter上告诉您的朋友有关此帖子的信息

翻译自: https://www.html" title=php>phpied.com/html" title=javascript>javascript-include-ready-onload/

html" title=javascript>javascript 传奇


http://www.niftyadmin.cn/n/1306932.html

相关文章

dojo省份地市级联之地市Dao接口类(四)

dojo省份地市级联之地市Dao接口类 CityDao.java&#xff1a; /*** 地市*/ package com.you.dao;import java.util.List;import com.you.model.City;/*** 地市Dao* author YHD* date 2014-02-15**/ public interface CityDao {/*** author YouHaidong* param provinceCode* ret…

python super()函数的理解

Python是一门面向对象的语言&#xff0c;定义类时经常要用到继承&#xff0c;在类的继承中&#xff0c;子类继承父类中已经封装好的方法&#xff0c;不需要再次编写&#xff0c;如果子类如果重新定义了父类的某一方法&#xff0c;那么该方法就会覆盖父类的同名方法&#xff0c;…

dojo省份地市级联之省份Dao实现类(五)

dojo省份地市级联之省份Dao实现类 ProvinceDaoImpl.java&#xff1a; /*** */ package com.you.dao.impl;import java.util.ArrayList; import java.util.List;import com.you.dao.ProvinceDao; import com.you.model.Province;/*** author YHD**/public class ProvinceDaoImpl…

Python 更好的日期时间模块arrow

Python中有很多时间和日期处理的库&#xff0c;有time、datetime等&#xff0c;虽然提供了很完整的对日期、时间以及时区转换处理的功能&#xff0c;但是方法过多&#xff0c;不易于记忆&#xff0c;而且经常需要各种转换操作&#xff0c;非常繁琐&#xff0c;比如时间和时间戳…

Extjs 4.0 Tab页

1、JSON代码 Ext.MyTabsExt.extend(Ext.TabPanel ,{ xtype:"tabpanel",activeTab:2,width:694,height:571,initComponent: function(){this.items[{xtype:"panel",title:"学生",autoScroll:false,width:703,items:[{xtype:"grid",titl…

在许多WebPagetest浏览器中运行jsperf测试

动机(Motivation) 1. You write a new test to confirm a JavaScript-related performance speculation 2. You click 3. Your test runs in a bunch of browsers 1.您编写了一个新测试以确认与JavaScript有关的性能推测。2.单击3.您的测试在许多浏览器中运行 词汇表(Glossary)…

6月中国编程语言排行榜新鲜出炉,Python要“篡位”了?

TIOBE 6 月榜 从6月榜单可以明显看出&#xff0c;第二名的 Python 与第一名 C 语言之间的差距与前几个月相比正在逐渐缩小&#xff0c;从上个月的 1.51% 缩小到了本月的 0.7%。 TIOBE 官方对于这一现象也做出了预测&#xff0c;认为Python 即将登上TIOBE 榜单第一名的位置。与…

Extjs 4.0 Window

1、JSON代码 Ext.MyWindowExt.extend(Ext.Window ,{ xtype:"window",title:"我的窗口",width:710,height:401,autoHeight:false,autoScroll:true,disabled:false,hidden:true,stateful:true,autoDestroy:true,hideBorders:true,bodyBorder:true,border:tru…