• Resolved greencode

    (@greencode)


    I want to tidy my header.php file up a bit and so I’m creating a custom.js file to add all of the lose javascript etc. The problem is when it’s in the header it works okay but if I then add it to an external file and link to that from the header it doesn’t work?

    For example I’m using

    <script type="text/javascript" src="<?php bloginfo( 'template_directory' ); ?>/scripts/custom.js"></script>

    to call the javascript and it’s linking correctly but the code isn’t working. An example of the code not working:

    jQuery(document).ready( function($) {
    	$('#access > li:last-child').addClass('last-item');
    } );

    I’ve tried moving the location of the link around the head tags but nothing!

    Any help would be very much appreciated.

Viewing 12 replies - 1 through 12 (of 12 total)
  • Can you try with an absolute URL for the javascript location, instead of a relative URL ?

    Thread Starter greencode

    (@greencode)

    Thanks for your help. But what I’m currently using (the code above) converts that into absolute path to the script?! I’ve tried typing in the actual url to the script, just in case that wasn’t working, but that still doesn’t work.

    Thread Starter greencode

    (@greencode)

    Bump

    Thread Starter greencode

    (@greencode)

    Thanks for your help but I’ve added the script as per their example:

    <script type="text/javascript" src="<?php bloginfo('template_url'); ?>/pathto/yourscript.js"></script>

    And it still doesn’t work. The only way I can get it to work is by adding the actual script to the header file but that’s gonna get very messy when I want to add more and more scripts!

    Thread Starter greencode

    (@greencode)

    Anyone?

    Hi there!

    I used the same way as yours to refer to an external javascript inside my header.php, and it worked well for me, just now. If it’s not working, maybe it’s your script that has a problem…?

    Hmm.. Try putting an alert(‘any string’) to the first line of the function inside the script you are calling and test if an alert msg does appear. It’s what I always do to check if my script’s fine.

    Hope this could help.. ??

    Thread Starter greencode

    (@greencode)

    Hi. Thanks for your help. What do you mean about adding an alert to the first function?

    gtquila means you should add some code to your javascript file (like an alert box) to confirm that it’s working.

    https://www.w3schools.com/JS/js_popup.asp

    My header.php:

    ...
    <script type="text/javascript" src="<?php bloginfo('template_url'); ?>/plugins/js/form-checker.js"></script>
    ...

    Somewhere in my sidebar.php:

    ...
    <form..onsubmit="return validateForm()">
    </form>
    ...

    My script inside template_url/plugins/js/form-checker.js:

    ...
    function validateForm(){
    alert("test")
    }
    ...

    That’s it!

    Thread Starter greencode

    (@greencode)

    Thanks so much to both of you. I just added a checker script and managed to find the problem. It was a function that I’d included that decided to break everything else! Good idea though to add a very simple script and make sure that’s working fully in order to check more complex scripts.

    Glad to have helped! ??

Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘External Javascript not working’ is closed to new replies.