While you can’t use Javascript with WordPress.com blogs, you can with the full version of WordPress.
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 WordPress Codex 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:
- Web Reference – Professional Ajax: XML, XPath, and XSLT
- How to Auto Include a Javascript File
- Listing of Javascript Programming Articles from Web Reference
Related Articles
- Using Javascript in WordPress
- Looking For Javascript Libraries?
- Top 9 Javascripts for Your Website or Blog Design
- Code Snippets – Help and Cheating and Goodness
- HTML, CSS, PHP, and More Cheat Sheets
- More Must-Have Bookmarklets Than You Can Swing a Browser At
- WordPress Hooks – All of Them
- WordPress Hooks
- Taking Your Camera on the Road – Helpful Website Javascripts
Site Search Tags: javascript, code, php, wordpress, scripts
Copyright Lorelle VanFossen
5 Comments
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
@ 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. 😀
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?
@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.
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
[…] How to Put Javascript in External Files […]
[…] How to Put Javascript in External Files […]
[…] How to Put Javascript in External Files […]
[…] How to Put Javascript in External Files […]