In the past 6 months, I have proposed many examples using calculated columns to write HTML. If you are not familiar with this method, I strongly recommend that you first check out the post that started it all before reading the rest of this article.

Warning! This article doesn’t offer any plug and play example. Wait for my next article to see the method at work.

The HTML calculated column method has allowed us to include ”div”, “a”, “img”, “iframe”,”table” and other tags in our formulas. Now, have you tried to use “script” tags? If it worked, this would open up a whole new world of opportunities!

Well, you sure can include “script” tags in your formulas, and like for the other tags the HTML calculated column method will convert them from text to actual HTML. However, there is a significant limitation: the script will be included in the page, but not executed, which makes this pretty much useless…

Fortunately, this issue is not new, and JavaScripters have come up with a workaround. Imagine you want to inject and run SomeJavaScriptCode in your Web page.
If we inject this:

<script type="text/javascript">SomeJavaScriptCode</script>

The script will not execute.

If now we inject this:

<img src="SomePicture.gif" onload="SomeJavaScriptCode" />

The browser will load SomePicture.gif, and then it will trigger the script attached to the onload event. Et voila! We have a method that allows us to write scripts in calculated columns and force the browser to run them. The trick is to use the onload event, an event that only works with some HTML tags like “body”, “iframe” and “img”.

Note: I have already used the onload event in some other posts, for example here, combined with an iframe.

The technique described in this post will open new possibilities to our “HTML calculated column”. I certainly don’t want to push this too far – we are talking about a workaround on top of another workaround – but it will come in handy to answer some simple requests. Well see an example in my next post.

Advertisement