Gorgo.Live.ToString()

Mariusz, Gorzoch tech Blog

Hint around Internet Explorer and Javascript

leave a comment »

Lately I was working on one old project build on top of classic ASP. What I found about it was that application was so slow during rendering in client browser. On the end I found that reason for that was JavaScript inline function hooked to “onClick” event. I found that if you have a lot of let say rows generated in table and each row contain “onClick” even, which contain JavaScript with differs literally from JavaScript in other rows then it would take forever to show result to the client:

BAD APPROCHE:

<table>
    <tr onClick=’someFunction(1)’><td>1</td></tr>
    <tr onClick=’someFunction(2)’><td>2</td></tr>
    <tr onClick=’someFunction(3)’><td>3</td></tr>
    <tr onClick=’someFunction(4)’><td>4</td></tr>
    <tr onClick=’someFunction(5)’><td>5</td></tr>
    …
    <tr onClick=’someFunction(9999)’><td>9999</td></tr>
</table>   

GOOD APPROCHE:

<table>
    <tr param="1" onClick=’someFunction(this.param)’><td>1</td></tr>
    <tr param="2" onClick=’someFunction(this.param)’><td>2</td></tr>
    <tr param="3" onClick=’someFunction(this.param)’><td>3</td></tr>
    <tr param="4" onClick=’someFunction(this.param)’><td>4</td></tr>
    <tr param="5" onClick=’someFunction(this.param)’><td>5</td></tr>
    …
    <tr param="9999" onClick=’someFunction(this.param)’><td>9999</td></tr>
</table>   

(please notice that in ‘Good approach’ JavaScript function in onClick event is the same for all rows <- and this is a hint, which speed up rendering)

the final result is the same, but in case of a lot of rows you can find that “GOOD APPROCHE” will take much less time for the browser to show results.

coding can be sometimes tricky and without hints it can lead you to strange results and behavior. Sometimes when ‘I’m looking at “cooking made by my son” I think he can be a good programmer in the future and not because what he get on the end… but rather by the tricks he used to make this think look like a nice pace of chocolate cake.

🙂 happy coding

Advertisements

Written by Mariusz Gorzoch

9 May 2010 at 21:29

Posted in Bez kategorii

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: