SharePoint JavaScript and <script …/>

by dave Sun, June 19 2011 04:53

One of the hardest things to deal with when working with JavaScript is its fragility.  Not the language itself, but much of the large libraries we depend upon and the various interpretations of JavaScript implemented in the various browsers and even the servers themselves.  Conflicts and contradictory details lead to a lot of head-scratching and wasted time.  Add to that the generally poor state of documentation and you can see why so much time is wasted.

I recently had an issue where one innocuous little change, that technically isn’t even really a functional change, can wreak just such havoc.

When working with JavaScript in SharePoint, this:

<script type="text/javascript" src="/SiteAssets/MyScript.js" />

and this

<script type="text/javascript" src="/SiteAssets/MyScript.js" ></script>

are functionally equivalent but they lead to drastically different outcomes.  The latter is correct.  The former will cause you to get the following error:

Microsoft JScript runtime error: Unable to get value of the property 'onsubmit': object is null or undefined

on this line in ScriptResource.axd: this._onsubmit = this._form.onsubmit;

Tags:

Development | SharePoint 2010 | JavaScript