How to have a script in the <head> add script at the end of the <body>

后端 未结 5 1466
深忆病人
深忆病人 2020-12-07 00:51

A client is using Sharetribe which allows you add custom JS via admin, but only in the head. I want my script to load after jQuery, but jQuery is loaded at the end of the bo

5条回答
  •  遥遥无期
    2020-12-07 01:09

    Use DOMContentLoaded event:

    The DOMContentLoaded event is fired when the document has been completely loaded and parsed, without waiting for stylesheets, images, and subframes to finish loading (the load event can be used to detect a fully-loaded page).

    document.addEventListener("DOMContentLoaded", function (event) {
      console.log("DOM fully loaded and parsed");
    
      // Your code here
    });
    

    DOMContentLoaded is same as ready event of jQuery.

    Documentation

提交回复
热议问题