How Do I Add jQuery To Head With JavaScript?

后端 未结 3 2080
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-06 22:49

I am trying to include jQuery to an HTML page conditionally. It only needs to be added if it doesn\'t exist yet.

I am using the following code near the top of my bod

3条回答
  •  庸人自扰
    2020-12-06 22:50

    You are not waiting for the script to load after appending it to document

    Try this code before appending:

       function helper(){
            if (typeof jQuery === 'undefined') {
                alert('jquery still not present :(');
            }
       };
    
       script.onreadystatechange= function () {
          if (this.readyState == 'complete') helper();
       }
       script.onload= helper;
    

    I've found it here if you want to know more

    Also there are loaders like StealJS or YepNope

提交回复
热议问题