在javascript网页小部件中加载Bootsrap js

时间:2017-09-15 07:24:44

标签: javascript jquery twitter-bootstrap web-widget

我正在尝试使用js构建Web小部件。我想在用户的网站上显示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

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

  1. TypeError:$(...)。modal不是函数
  2. 错误:Bootstrap的JavaScript需要jQuery
  3. 在数据中我正在返回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>'));
    

    任何帮助将不胜感激。

    感谢。

1 个答案:

答案 0 :(得分:0)

我已经通过简单地删除

解决了这个问题
var jQuery;

来自我的代码。原因是bootstrap JS寻找全局变量jQuery。所以当用户页面中没有jQuery时,它将无法找到该全局变量并将抛出错误

错误:Bootstrap的JavaScript需要jQuery

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

干杯。