1

I am trying to use the Play CDN of TailwindCSS on a page that is using jQuery, and it gives an error. Anyone has the same issue? Maybe I am making something obviously wrong that I can't spot?

I cleaned up the code and prepared just a demo to illustrate this issue:

$(document).ready(function () {
  alert("Hello world");
});
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="https://unpkg.com/@tailwindcss/[email protected]"></script>

This page should display an alert box when loaded, but instead it gives the following error. The above code should create a page that shows an alert, but its not working, giving JS error:

Uncaught TypeError: $(...).ready is not a function

1

1 Answer 1

0

Update

I reported the bug to the TailwindCSS team in tailwindlabs/tailwindcss #15977. It will be fixed in the next release.

Our @tailwindcss/browser build is intended to run inside a <script> tag inside browsers. Because of how variable assignment within <script> tags work, all variables that were defined within that script are currently assigned on the global namespace.

This is especially troublesome as eslint uses $ as a valid mangling character so importing the CDN build would now redefine globalThis.$ which collides with many very popular JavaScript libraries.

In order to avoid polluting the global namespace, this PR changes the build step to emit an IIFE (so all vars defined are scroped to the function closure instead of the global namespace).

Fixed from v4.0.1 onwards.

$(document).ready(function () {
  alert("Hello world");
});
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="https://unpkg.com/@tailwindcss/[email protected]"></script>

Original (reproducible in the 4.0.0 version only)

It looks like the TailwindCSS v4 CDN also uses the $() function. I think it might be best if you stop using it and switch to the jQuery() function, or declare another shorthand function.

The last declared $() function will always run. Since you loaded TailwindCSS second, jQuery's $ function got overwritten. Reversing the order isn't a solution either, because in that case, TailwindCSS would lose its $() function.

console.log($)
<script src="https://unpkg.com/@tailwindcss/[email protected]"></script>

Solutions

Instead of $(), use the jQuery() function:

jQuery(document).ready(function () {
  alert("Hello world");
});
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="https://unpkg.com/@tailwindcss/[email protected]"></script>

Or, you can restore the j() (or some similar shorthand function) functionality using jQuery() function.

// Restore "j()" (instead of "$()") to work as jQuery
window.j = window.jQuery = jQuery.noConflict();

j(document).ready(function () {
  alert("Hello world");
});
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="https://unpkg.com/@tailwindcss/[email protected]"></script>

The error did not occur when using TailwindCSS v3, by the way. Since the v3 CDN did not include the $() function.

$(document).ready(function () {
  alert("Hello world");
});
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="https://cdn.tailwindcss.com"></script>

Sign up to request clarification or add additional context in comments.

1 Comment

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.