« Home | Digital : Joy Division » | Dead Can Dance Videos » | Joy Division - Love will tear us apart » | Sweet Dreams, Internet... Sleep Tight! » | links for 2006-04-20 Men hallucinate after eatin... » | links for 2006-04-19 Life, the Universe, and Eve... » | links for 2006-04-18 Adding javascript to Blogge... » | Picos da Europa » | 80s Music Videos on YouTube » | links for 2006-04-17 Ph.D. Thesis Proposal: A Ne... » 

Friday, April 21, 2006 

Executing JavaScript code inside Blogger posts

Now this is a really cool hack, and it runs on both IE and FireFox. As you may have noticed, Blogger does not allow for <script> tags inside posts. However, a workaround is to use DOM scripting and the JavaScript eval() function, in order to execute the code inside some other tag. Just place something like this in the HEAD part of your Blogger template:

<script type="text/javascript">
function loadCodePosts() {
var c, i;
if (document.all)
c = document.all.tags('code');
else if (document.getElementsByTagName)
c = document.getElementsByTagName('code');
var junk = /^\s*\46lt;\133\133CDATA\133|]]\46gt;\s*$/g;
for( i=0; i < c.length; i++ ) {
try { eval( c[i].innerHTML.replace( junk, '' ) ); } catch (err) { };
}
}
</script>

Also on your template, place the loadCodePosts() function on the onload property of the BODY tag:

<body onload="loadCodePosts()">

Finally, on your posts, just do something like this in order to include JavaScript code:

<code style="display:none"> Your JavaScript code! </code>

That's it... Simple and easy ^_^

Check this for learning how to place Google maps on Blogger posts.

About me

www.flickr.com
This is a Flickr badge showing public photos from Bruno Martins. Make your own badge here.

Listening to


 All the Web
Me at BookCrossing
Campos Magneticos

Previous posts

Friendly Blogs

Powered by Blogger, Flickr
and del.icio.us