jQuery's load method designs dynamic loading and resolves loaded page js

The jQuery load() method is a simple and powerful ajax method.
load() retrieves data from the server and places the returned data in the selected element.grammar
$(selected).load(URL,data,callback);

Where:
URL s are required, and consecutive are optional. The callback parameter is the function that the load() method is required to execute after execution.
For example:
//This is what loads the test text read from the txt file into the DIV with id div1
$("#div1").load("txt/test.txt");
With callback function:

$("#btn").click(function(){
    $("#div1").load("txt/text.txt",function(responseTxt,statusTxt,xhr){
        if(statusTxt=="success"){
            alert("Loading succeeded");
            alert(responseTxt);
        }
        if(statusTxt=="error"){
            alert("error:"+xhr.status+":"+xhr.statusTxt);
        }
    });

});
In the code above:
1. responseTxt: Contains results when the call succeeds
2. statusTxt: Contains call status
3. xhr: Contains the XMLHttpRequest object.

Practical application
:

sy.showUrlTab=function(id,title,url,pid){
    /**
     * 1:Left menu click jump function
     */
    url = "/projectName-html"+url;
    $("#mainContent").load(url,function(result){
        /**
         * Dynamic loading of js files
         * Solution load js cannot debug
         * Note that the naming rule html has the same name as the js file name
         */
        var script = document.createElement("script");
          script.type = "text/javascript";
          if(typeof(callback) != "undefined"){
            if (script.readyState) {
              script.onreadystatechange = function () {
                if (script.readyState == "loaded" || script.readyState == "complete") {
                  script.onreadystatechange = null;
                  callback();
                }
              };
            } else {
              script.onload = function () {
                callback();
              };
            }
          }
          script.src = url.replace('.html','.js')+"?version="+sy.version;
          document.body.appendChild(script);
    });
//  $.getScript("admin/qrcode/qrcodeList.js?version=20171213");
    /*document.write('<script src="admin/operators/operatorsInfo.js?version=20171213"></script>');*/
    ps:  url: mainContext Page url Full Path
        id: mainContext Of id
        pid:mainContext Superior menu Of id

Tags: JQuery Javascript QRCode

Posted on Fri, 05 Jun 2020 13:13:53 -0400 by dougmcc1