Search code examples
javascriptphpjquerysrc

Included JQuery not Working when using <script src="">


I'm echoing the contents of a JS file with PHP (makes me feel icky) to get around the fact that I'm unable to use the definitions in it otherwise. The includes are in the head.

<!--this works... the correct way doesnt-->
        <script>
            <?= require_once($base_path . "/public_html/js/validation.js"); ?>
        </script>
<!--todo this does not work-->
        <script type="text/javascript" src="/js/validation.js"></script>

I've checked the response and it is 200 OK.

enter image description here

Other JS files I've included this way work. The file is JQuery that requires $(document).ready(). A snippet of the JS I'm trying to include is as follows...

$(document).ready(function () {

    $('body').on("change keydown paste input", 'input[type="tel"]', function () {
        var oldtel = $(this).val().replace(/[^0-9]/g, '');
        $(this).val(oldtel);
        if ($(this).val().length > 3) {
            var tel = oldtel.slice(0, 3) + "-" + oldtel.slice(3, 10);
            $(this).val(tel);
        }

        if (oldtel.length >= 7) {
            var tel = oldtel.slice(0, 3) + "-" + oldtel.slice(3, 6) + "-" + oldtel.slice(6, 10);
            $(this).val(tel);
        }
    });
    ...

Solution

  • A common problem which occurs due to outdated cache, reload with: ctrl + f5