简体   繁体   English

在 javascript web 小部件中加载 Bootsrap js

[英]Load Bootsrap js in javascript web widget

I am trying to build web widget using js.我正在尝试使用 js 构建 Web 小部件。 I want to display bootstrap modal on user's site.我想在用户的站点上显示引导模式。

Here is my code in my js file.这是我的 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

I can see all scripts and modal loading correctly in inspect elements.我可以在检查元素中正确看到所有脚本和模式加载。 But in console I get two errors.但是在控制台中我收到两个错误。

  1. TypeError: $(...).modal is not a function类型错误:$(...).modal 不是函数
  2. Error: Bootstrap's JavaScript requires jQuery错误:Bootstrap 的 JavaScript 需要 jQuery

In data I am returning json object.在数据中,我返回 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>'));

Any help will be appreciated.任何帮助将不胜感激。

Thanks.谢谢。

I have solved the issue by simply removing我通过简单地删除解决了这个问题

var jQuery;

from my code.从我的代码。 The reason is bootstrap JS looks for a global variable jQuery.原因是 bootstrap JS 寻找全局变量 jQuery。 So when jQuery is not present in user's page it won't be able to find that global variable and will throw an error因此,当用户页面中不存在 jQuery 时,它将无法找到该全局变量并抛出错误

Error: Bootstrap's JavaScript requires jQuery错误:Bootstrap 的 JavaScript 需要 jQuery

Hope this will help someone.希望这会帮助某人。 :) :)

Cheers.干杯。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM