Why is $(document).ready not firing for me?

Why is $(document).ready not firing for me?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Why is $(document).ready not firing for me?

You've included jQuery in your PHP file, placed a script tag containing $(document).ready(), and confirmed that removing the callback works fine. The alert never appears. This behavior usually stems from one of two specific execution order problems: either the DOM hasn't finished loading when the handler is attached, or the code you're trying to attach isn't actually running in the browser context at all.

The include directive is not a script tag

The most common culprit in your scenario is how the PHP include statement processes content. When you use include 'js.php', the server reads that file, concatenates its contents into your main page's output stream, and then sends the combined HTML to the browser. However, if there is any whitespace or text preceding the opening <script> tag in your generated output, or if the include happens before the DOM elements you are selecting exist, timing becomes critical.

Specifically, $(document).ready() waits for the DOM to be fully parsed. If your PHP page includes a large block of content before the script that initializes this handler, and that content contains HTML that prevents the parser from finishing immediately, you might hit an edge case where the event isn't triggered as expected. But more often than not, the issue is simpler: you are relying on $(document).ready when you should be relying on DOMContentLoaded.

Prefer DOMContentLoaded over ready

While $(document).ready() is convenient because it works regardless of whether external scripts have loaded, it waits for everything in the head (excluding stylesheets) to finish parsing. If you are including files via PHP and there's a chance that some resource loading blocks the event loop, switching to jQuery's native DOMContentLoaded handler can resolve the issue instantly.

Here is how you can refactor your code:

$(document).on('DOMContentLoaded', function() {
    alert("hello");
});

This event fires as soon as the DOM structure is complete, even if external scripts and stylesheets are still downloading. In many legacy setups involving include directives, this slight shift in timing ensures the callback executes before any subsequent scripts attempt to manipulate the page.

Verify execution context

If you skip the .ready() block and the code works immediately, it confirms that jQuery is loaded correctly and your syntax is valid. The problem is strictly about when the function runs relative to the DOM state.

However, there is a subtle trap involving how PHP renders files. If js.php is included early in your layout but references elements that are dynamically appended later via AJAX or other means, .ready() might fire before those elements exist. In your specific case where you just want an alert on load, this shouldn't be the issue unless you are selecting elements by ID that don't exist yet.

If you suspect the DOM is still parsing when the script runs, try wrapping your logic in a self-executing function that explicitly checks for completion:

(function($){
    $(function(){
        alert("hello");
    });
})(jQuery);

This pattern isolates the scope and ensures the callback is tied directly to the document's lifecycle events.

Modern alternatives

If you are maintaining older codebases where jQuery is essential, stick with .ready(). But if you are greenfield or refactoring, consider dropping jQuery entirely for vanilla JavaScript. The native event listener is more performant and doesn't require a library:

document.addEventListener('DOMContentLoaded', function () {
    alert("hello");
});

This approach removes the overhead of loading jQuery just to handle document events. If you need further guidance on integrating these patterns into your Laravel application or managing asset inclusion, you might find helpful resources at https://laravelcompany.com. They often cover modern approaches to asset management that make timing issues like this a thing of the past.

Final check

Before assuming the DOM is the issue, ensure there are no syntax errors in your PHP file that prevent the script tag from being output correctly. Sometimes, a stray comment or echo statement before the <script> tag breaks the flow entirely. Clearing cache and checking the browser console for 404s on the jQuery file itself are also standard troubleshooting steps if the library isn't actually present in the DOM when your script runs.

Our network