Wednesday, August 25, 2010

SP2010 page reacting strange on javascript code through content editor webpart

I’m working on a SP2010 team site for a client. One of the requirements is a graphical view of the traffic news near the clients location. Of course you can do this through SPD2010 and calling a webservice (soap or REST). Another simpel way is adding some javascript code in a hidden content editor webpart. In the Netherlands the ANWB has a nice feature where you can call some javascript code within your HTML page. After this a graphical image is rendered with the most recent traffic news. The image looks like this:

image

What I did for my customer was adding a hidden content editor webpart on the page (mine was based on the wike template, but this issue happens also on the default default.aspx with webpart zones). Via HTML I selected the Edit HTML source within the content editor webpart.

image

After this I added the following bit of code…

<script type="text/javascript">
<!--     
      var anwb_tool="verkeersinformatie";
//-->
</script>
<script type="text/javascript" src="http://www.anwb.nl/verkeer/verkeerstool.js">
</script>

After a couple of seconds the image was rendered. I was a happy man and was already dreaming of drinking my beer when I got home, when I pressed ‘Save and Close’. Suddenly I had 2 rendered images… I returned back to the page, edited the content webpart again and pressed ‘Save and close’ again. Suddenly I had three (yes 3) rendered images… Apparently, every time you hit the ‘Save button’ SP2010 somehow renders the code again and adds another image in the webpart…..

image

Please try it out yourself. For the javascript code, please visit http://www.anwb.nl/binaries/doc/verkeer/verkeerstooltjes-code/code-zonder-kaart.txt and copy past the code in a content editor webpart in your SP2010 site.

Within the SP2010 Content Editor Webpart, you now have the option to link to a seperate file to include in your webpart. I put a .txt file in the assets library and typed in the url. This worked and the image is only rendered once.

image

Question is… Is this a bug (or feature) in SP2010 or just crappy coding from the anwb developer???