Search code examples
javascriptjqueryhtmlprototypejs

Identifying and fixing javascript/prototype/jquery conflicts?


I am experiencing an issue with my scripts to which I cannot find a proper fix. I have this code, which uses glassbox.js and all its extras, which uses to work (it displayed the glassbox appropriately) but since I added JQuery to the file it has stopped working. I am not sure how to rearrange or call the scripts so that it functions again. The commented out lines myBox.whatever are the lines causing the issue specifically:

@model OneEightyWebApp.Models.Centres
<link href="../../Content/Site.css" rel="stylesheet" type="text/css" media="screen" />

<head>
    <title></title>

    <script src="../../Content/javascripts/prototype.js" type="text/javascript"> </script>
    <script src="../../Content/javascripts/scriptaculous/effects.js" type="text/javascript"></script>
    <script src="../../Content/javascripts/glassbox/glassbox.js" type="text/javascript"></script>

    <script type="text/javascript">
        var spaces = @Html.Raw(Json.Encode(ViewData["spaces"]))
        function glassLoad() {
            path_to_root_dir = "../../Content/";
            var myBox = new GlassBox();
            myBox.init('myBox', '600px', '400px', 'hidden', '', true, true);
           // myBox.apos('300', '300px');
           // myBox.appear();
            alert("clicked");
        }

    </script>
    <script src="../../Content/javascripts/prototype.js"> </script>
    <script type="text/javascript">
        document.observe('dom:loaded', function () {
            $$("body")[0].on('click', ".class1", function () {
                var myBox = document.getElementById("myBox");
                myBox.style.display = "block";
                glassLoad();
            });
        });
    </script>

    <script src="../../Content/jquery.js"></script>
    <script type="text/javascript">
        jQuery(function () {
            var array = [];
            jQuery("#centres").change(function () {
                var selectedCentre = $("#centres option:selected").text();
                $.getJSON("/Centres/output?centreName=" + selectedCentre, function (results) {
                    var data = results;
                    document.getElementById("container2").innerHTML = "";
                    var div;
                    for (var i = 0; i < document.getElementById("centres").value; i++) {
                        div = document.createElement("div");
                        div.className = "class1";
                        div.innerHTML = "Shop " + data[i];
                        div.innerHTML += "<br>";
                        div.innerHTML += "Floor Space: <b>" + spaces[i] + " m2 </b>";
                        div.style.width = "100px";
                        div.style.height = "100px";
                        div.style.padding = "0px";
                        div.style.float = "left";
                        document.getElementById("container2").appendChild(div);
                    }

                });
            });
        });
    </script>

</head>

<body>
    <div id="container1" style="width: auto; height: 50px;">
        <button style="margin: auto; vertical-align: top; float: left; font-size: 16px;" type="button" onclick="glassLoad();">Generate</button>
        @Html.DropDownList("centres", (List<SelectListItem>)ViewData["centres"])
        <select id="mySelect"></select>
    </div>
    <div id="container2" style="margin: 10px; float: left;"></div>

    <div id="myBox" style="display: none; width: 600px; height: 400px;">
        <div id="exitButton" style="position: absolute; left: 564px; bottom: 173px; z-index: 1001;" title="close">
            <a href="javascript:THIS.fade();">
                <img id="exitImage" style="border: none;" src="../../Content/javascripts/glassbox/skins/exitButton.png"></a>
        </div>


    </div>

</body>

Solution

  • You have to put jQuery in noConflict and pass $ into the ready event here:

    $.noConflict();
    jQuery(function ($) {
      var array = [];
      ... // use $ from now on instead of jQuery
    

    That's one way of doing it, check the docs there are other patterns.