2009
09.11

I’ve been adding JavaScript code many times in my earlier blog posts. I know I struggled to do that. But I was too stubborn to google for clues because somehow what I did is working *GRIN*

Anyway, recently I struggled again, but this time I don’t have the energy to be stubborn, I googled for solution.

According to this article, this is the best way to incorporate JavaScript into WordPress blog post:

  1. Save your JavaScript code in a .js file
  2. Use SCRIPT tag to load the .js file
  3. Add another SCRIPT tag to execute any initiation functions

It will be something like this

<script type="text/javascript" src="/scripts/myscript.js"></script>
<script type="text/javascript">
<!--
	Initialize(); // With assumption that this function is defined inside myscript.js
-->
</script>

That’s all, very simple and straighforward indeed. So now looking back, what’s wrong with my earlier method anyway? Chronologically speaking, it would be as follows:

  1. I put the code directly inside a SCRIPT tag
  2. Then I realized that WordPress added <br/> to replace the newline, which basically prevent my JavaScript code running
  3. I removed the newline from my JavaScript code

Which is really horrible, considering that:

  1. my code is now unreadable to human 😀
  2. I must tirelessly replace all single line comments (// …. ) into multiple line style ( /* …. */ )
  3. It makes debugging with Firebug really difficult

Hmmpffhh… I learned my lesson, so let’s move on 😉 Did you ever make similar mistake because of your stubbornness?

GD Star Rating
loading...
How to Use Javascript in Your WordPress Blog Post, 5.0 out of 5 based on 1 rating

Incoming Search Term

Advertise Here

1 Trackback

 

No Comment

Add Your Comment