WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] External Javascript not working (13 posts)

  1. greencode
    Member
    Posted 3 years ago #

    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.

  2. sabinou
    Member
    Posted 3 years ago #

    Can you try with an absolute URL for the javascript location, instead of a relative URL ?

  3. greencode
    Member
    Posted 3 years ago #

    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.

  4. greencode
    Member
    Posted 3 years ago #

    Bump

  5. esmi
    Forum Moderator
    Posted 3 years ago #

  6. greencode
    Member
    Posted 3 years ago #

    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!

  7. greencode
    Member
    Posted 3 years ago #

    Anyone?

  8. gtquila
    Member
    Posted 3 years ago #

    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.. :)

  9. greencode
    Member
    Posted 3 years ago #

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

  10. Travis Northcutt
    Member
    Posted 3 years ago #

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

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

  11. gtquila
    Member
    Posted 3 years ago #

    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!

  12. greencode
    Member
    Posted 3 years ago #

    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.

  13. gtquila
    Member
    Posted 3 years ago #

    Glad to have helped! :)

Topic Closed

This topic has been closed to new replies.

About this Topic