1

I can't seem to pinpoint why this is failing - any ideas? -> See within .aspx

custom.master references / and script

<link rel="stylesheet" href="/admin/css/custom.css" />
<script src="/admin/js/jquery-1.9.1.min.js"></script>
<script src="/admin/js/jquery-1.10.2-ui.min.js"></script>
<link href="css/ui-lightness/jquery-ui-1.8.13.custom.css" rel="stylesheet"/>
<asp:ContentPlaceHolder ID="HeadPlaceHolder" runat="server"></asp:ContentPlaceHolder>

<script>
    $(function () {
        var $ = jQuery.noConflict(); <-- This here is the culprit

Within rPage.aspx (MasterPageFile="~/admin/custom.master")

<asp:Content ID="Content1" ContentPlaceHolderID="HeadPlaceHolder" runat="server">
    <link href="css/bootstrap-3.3.5.min.css" rel="stylesheet" />
</asp:Content>

<script>
    $(function () {
        var $ = jQuery.noConflict(); //**Without this line the next line fails**
        $("#<%= txtStartDate.ClientID %>").datepicker();

Error:

from console: TypeError: $ is not a function rPage:380:13

from debugger firefox dev edition:

378 $(function () {
379        //var $ = jQuery.noConflict();
380        $("#ContentPlaceHolder_txtStartDate").datepicker();
11
  • 3
    @RoryMcCrossan One of them is jQuery ui Commented Mar 17, 2016 at 16:31
  • 3
    @mkaatman I need better glasses XD Commented Mar 17, 2016 at 16:31
  • In your first example, it works because you've assigned jQuery to a local variable, i.e., not a global. So your second example would need the same thing unless you redefined $ to be jQuery in the global scope. Commented Mar 17, 2016 at 16:32
  • 4
    Presumably $ is being overwritten at some point between line 378 being executed and the DOM Ready event triggering the function that includes line 380. You haven't provided enough code to tell where. Commented Mar 17, 2016 at 16:34
  • 3
    Seeing as $(function() { works, and jQ passes a reference to itself as the first argument to the callback, you can write $(function($) { or jQuery(function($) {, which is the more common approach when you have conflicts. It's likely that the $ name in your $(function() { isn't the jQ object to begin with (prototype uses $, too I think) Commented Mar 17, 2016 at 16:35

1 Answer 1

2

Looking at the (currently) posted example, I see no reason for the NoConflict call. I am assuming it is because maybe the other library isn't (yet) shown in your sample code.

That said...

Try moving your NoConflict declaration outside of your Immediately-Invoked Function Expression (IIFE) & right BEFORE you bring-in your other library..

Like so...

<script>
$.noConflict();
// Code that uses other library's $ can follow here.

$(function () {
     // Do awesome stuff here...
})();
</script>

I also suggest...

Aliasing jQuery into your IIFE's like so...

<script>
    $(function ($) {
         // Do awesome stuff here...
    })(jQuery);
</script>
Sign up to request clarification or add additional context in comments.

2 Comments

One quick question - what with the extra })(); before ending the script?
See my update. You can alias arguments passed into your IIFE (if you like) - Cheers!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.