Skip navigation

How to Put Javascript in External Files

While you can’t use Javascript with blogs, you can with the full version of .

Before WordPress, I used to use a lot of Javascript over and over throughout my main site for a variety of reasons. Some of the Javascript was reused on every page to generate links and redundant information and processes. The Javascript code bulked up each web page. If I wanted to make a change in the Javascript code, I’d have to do a search and replace through hundreds of pages of code. It was a nightmare, until I learned how to put Javascript in an external file so I could call the file and code into each page instead of having all the code on every page.

Web Reference has a basic tutorial on how to put Javascript in external files to help you learn how to move the Javascript out of your template files. If you need to change the Javascript code, you just change the one file and it will be modified across all the page views with that Javascript in use.

Javascript can be included in WordPress posts as well as in WordPress Themes, it just needs some special handling. Javascripts from within the posts need to be called in from external files as WordPress formatting can strip away all the code, making the Javascript not function. Many WordPress Plugins include Javascript, and they embed the scripts and script callouts into your WordPress Themes using the many WordPress hooks.

The articles on the called Using Javascript and my article on Using Javascript in WordPress will help you with the process of including Javascript in WordPress.

AJAX, the new Javascript technology that is sweeping the web, can also be included with WordPress. Web Reference offers the following articles that may help with AJAX and Javascript techniques:

Related Articles


Site Search Tags: , , , ,
Copyright Lorelle VanFossen

5 Comments

  1. Posted May 5, 2008 at 5:44 pm | Permalink

    Wow thanks for this info I was getting ready to get a diff blog acct. due to not being able to add the java.

    One question . So if I want to add java on my sidebar in a widget first I add

    and then the java code?

    hoganfe

  2. Posted May 5, 2008 at 7:59 pm | Permalink

    @ hoganfe handmade handbag originals:

    Java or Javascript. Totally different animals. The widget has to be there in order to put something in it. It’s like you need an envelope in order to put a piece of paper in the envelope. :D

  3. Jackie
    Posted November 14, 2010 at 8:32 am | Permalink

    I am replying to an older post. But hopefully, somone will answer. I need to have a wp page with javascript in the post. Is there a plugin that would work with the current version of WP?

    • Posted November 17, 2010 at 1:07 pm | Permalink

      @Jackie: The age of a post has no relevance on responses. The instructions above are still current. There are Plugins, but they are not necessary.

  4. Posted November 24, 2010 at 6:10 pm | Permalink

    Here’s a plugin I developed which enables you to incorporate Javascript components- specifically ExtJS components or snippets directly in your WordPress posts or pages.


4 Trackbacks/Pingbacks

  1. [...] How to Put Javascript in External Files [...]

  2. […] How to Put Javascript in External Files […]

  3. […] How to Put Javascript in External Files […]

  4. […] How to Put Javascript in External Files […]

Post a Comment

Follow

Get every new post delivered to your Inbox.

Join 20,969 other followers

%d bloggers like this: