2009年4月7日星期二

Add JavaScript code to posts on Blogger


I tried to add some JavaScript code in my post, but it seemed to be harder than I thought. I googled a lot of pages about it and found pages like [1], [2] and [3]. For JavaScript code that not run immediately after the page is loaded, using plan <script> tag is ok. But my JavaScript code needs to be executed after the page is loaded, I tried the approach using <code>, it didn't work. Then I decided to use window.setTimeout to hack that. It works.


See the script below, the content of "viewport" is modified by script after the page is loaded. Note, the script should be only one line to avoid <br> tags inserted by Blogger.

<script type="text/javascript">function fn() {var ele = document.getElementById("test");if (ele) {ele.innerHTML = "Content set by script ";window.clearTimeout(arguments.callee._timeout);}else {arguments.callee._timeout = window.setTimeout(arguments.callee, 500);}}fn();</script>
<span id="test"></span>



The script in action:


没有评论:

Technology changes life