在 JavaScript 网络小部件中加载 Bootstrap js

Load Bootsrap js in JavaScript web widget

我正在尝试使用 js 构建网络小部件。我想在用户站点上显示 bootstrap 模式。

这是我的 js 文件中的代码。

    (function () {
    
        // Localize jQuery variable
        var jQuery;
    
        /******** Load jQuery if not present *********/
        if (window.jQuery === undefined || window.jQuery.fn.jquery!== '1.12.4') {
            console.log("jQuery LOADED");
            var script_tag = document.createElement('script');
            script_tag.setAttribute("type", "text/javascript");
            script_tag.setAttribute("src",
            "https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js");
    
    
            // Try to find the head, otherwise default to the documentElement
            (document.getElementsByTagName("head")[0] || document.documentElement).appendChild(script_tag);
    
    
            if (script_tag.readyState) {
                script_tag.onreadystatechange = function () { // For old versions of IE
                    if (this.readyState == 'complete' || this.readyState == 'loaded') {
                        console.log(window.jQuery.fn.jquery);
                        scriptLoadHandler();
                    }
                };
            } else {
                console.log("ONLOAD STATE");
                script_tag.onload = scriptLoadHandler;
            }
        } else {
            // The jQuery version on the window is the one we want to use
            jQuery = window.jQuery;
            main();
        }
    
        
        /******** Called once jQuery has loaded ******/
        function scriptLoadHandler() {
            // Restore $ and window.jQuery to their previous values and store the
            // new jQuery in our local jQuery variable
            jQuery = window.jQuery.noConflict(true);
            // Call our main function
    
            main();
        }
    
        /******** Our main function ********/
        function main() {
            jQuery(document).ready(function ($) {
                /******* Load Bootstrap JS *******/
                var bootstrap_script = document.createElement('script');
                bootstrap_script.setAttribute("type", "text/javascript");
                bootstrap_script.setAttribute("src",
                        "https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js");
                        
                (document.getElementsByTagName("head")[0] || document.documentElement).appendChild(bootstrap_script);
                
                /******* Load Bootstrap CSS *******/
                var bootstrap_css_link = $("<link>", {
                    rel: "stylesheet",
                    type: "text/css",
                    href: "https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"
                });
                bootstrap_css_link.appendTo('head');
    
                /******* Load HTML *******/
                var jsonp_url = "example.com/srtest?callback=?";
                $.getJSON(jsonp_url, function (data) {
                    $("#myModal_srsr").modal("show");
                });
            })
        }
    
    })(); // We call our anonymous function immediately

我可以在检查元素中正确看到所有脚本和模态加载。但是在控制台中我得到两个错误。

  1. TypeError: $(...).modal 不是函数
  2. 错误:Bootstrap 的 JavaScript 需要 jQuery

在数据中我返回 json 对象。

    $data = json_encode(array("html"=>'<!-- Modal -->
    <div id="myModal_srsr" class="modal fade" role="dialog">
      <div class="modal-dialog">
    
        <!-- Modal content-->
        <div class="modal-content">
          <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal">&times;</button>
            <h4 class="modal-title">Modal Header</h4>
          </div>
          <div class="modal-body">
            <p>This is awesome.</p>
          </div>
          <div class="modal-footer">
            <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
          </div>
        </div>
    
      </div>
    </div>'));

我已经通过简单地删除

解决了这个问题
var jQuery;

来自我的代码。原因是bootstrap JS寻找一个全局变量jQuery。因此,当用户页面中不存在 jQuery 时,它将无法找到该全局变量并抛出错误

错误:Bootstrap 的 JavaScript 需要 jQuery

希望这会对某人有所帮助。 :)

干杯。