Getting javascript to run in a WordPress blog entry

It’s not as easy as it looks to get javascript to run from inside a WordPress post. For a recent post, I wanted to run a one-off javascript so I didn’t want to change the header.php file in my theme. Instead I found this info on the WordPress codex on how to call javascript from inside the <body> tags.

Also the javascript that I was originally working with used

<body onload="init()">

to start the javascript when the page loaded but this wouldn’t work from a WordPress blog entry. So I had to change my javascript from:

function init() {
something();
goes();
here();
}

to:

window.onload = function init() {
something();
goes();
here();
}

and then I call the init() function in my blog post using:

<script type="text/javascript">
init();
</script>

*Important*
I can to change my WordPress user settings to disable the visual text editor since it was adding/stripping various parts of the html and breaking it.

Comments on this article can be sent to me by e-mail..

Comments are closed.