Using Body OnLoad with SharePoint (2010)

I spent a decent amount of time trying to workaround not being able to access the body onload event of a SharePoint page, including looking at adding JavaScript event handlers programatically, before I came across this… almost an aside in another article.

In order to work around this limitation, SharePoint provides the “_spBodyOnLoadFunctionNames” array. When the body is loaded, the onload event handler executes each function whose name is contained in this array.

So apparently all one has to do to have a JavaScript function execute when the body loads is to add the name of the function (as a string) to that array, a la _spBodyOnLoadFunctionNames.push("functionName"); – simples!

D’oh!

In the end we just wanted to do to much for simple JavaScript so used the awesome jQuery library – which includes the $(document).ready() method to take care of that, but I’m sure the above will help somebody, somewhere, some day.

Solved: IE event – “this.parentNode.parentNode is null or not an object”

… it bloody-well is! This post was very nearly “Things that piss me off 5: Internet Explorer” (that one may yet come) but I thought a more descriptive title would help more people struggling with the same problem to find it.

Anyway: background.  I am writing a new feature for a Content Management System to allow users to upload files. With a simple bit of javascript I added a “+” button to allow them to add new rows to the form, allowing them to upload multiple files. It looks a bit like this:

Screenshot of Upload Images feature
Screenshot of Upload Images feature

Anyway, it would obviously be nice to allow the user to remove a row if they’ve added one or two too many. The obvious solution seems to be to add a “-”  button beside each. Easy, right?

Unfortunately Internet Explorer, as per usual, ballses everything up. Because each – button is created dynamically I have to add an EventListener using JavaScript code. No problem so far. That event listener is a simple, one-line:

this.parentNode.parentNode.removeChild(this.parentNode);


As each row (the label, field itself (including button) and “-” link) is inside a div, the above code should remove that div and therefore remove the row. And it does… in Firefox. Try it in Internet Explorer and you get a message telling you “Error: ‘this.parentNode.parentNode’ is null or not an object”.

Whhhaaa?!

Don’t you just hate when you can easily find something your code claims it can’t? Am I the only one who screams “It’s right f**king there!!” at my monitor? In this case, the problem seems to boil down to IE’s supid, proprietary, fucked up way of attaching Event listeners. When you use Mozillas the W3C standard addEventListener method to attach a listener to an event, “this” in the event handler refers to the object which the event is called on. Obvious, yes?  However when you use IE’s attachEvent instead, IE doesn’t seem to act in the same way. Apparently, instead, IE sets “this” in an event handler to the window object instead of the calling/event object! Another good job from Micro$oft!

Apparently it’s can be overcome, but that involves a lot of code and I refuse in principle to do that much inelegant hacking for something so simple. It’s much easier (and principled) to do this:

IE error message: "Sorry, your browser doesn't support this function. This is probably because you're using Internet Explorer. www.getFirefox.com"
Not really a long-term solution, but bloody satisfying nonetheless.

Big PHP Niggle

I need a quick bitch.

I love PHP. It’s everywhere. All my sites are written in it. My CMS is written in it. My blog engines, WordPress and b2evolution, are written in it. It’s free. It runs on any platform. Hosts all support it. With PHP 5 they’ve even done a lot of work on PHP4’s main downfall – lack of OOP support.

One thing really pisses me off though – it’s totally inconsistent.  Consider these two functions:

  1. strstr — Find first occurrence of a string
  2. in_array — Checks if a value exists in an array

Take a close look. See the signatures?

string strstr ( string $haystack , mixed $needle [, bool $before_needle ] )
bool in_array ( mixed $needle , array $haystack [, bool $strict ] )

In in_array the first parameter is what you’re looking for and the second is what you’re looking in (more or less consistent with preg_match).  In strstr (and its derivatives) this order is reversed. Who on Earth let that slip through? I’m yet to find a free text editor that will tell you which parameters a given function expects (Dreamweaver does a decent job but it’s bloody expensive for a text editor!) the way Visual Studio does for C#, which I use a lot in work.  Given that situation, you’d expect consistency in this sort of thing.  Apparently not so. Instead, every time I want to use one of these functions I have to do a quick Google to bring me to the PHP manual page to tell me which order the parameters come in.

Of course they can’t even fix that without breaking backwards compatibility either, so we’re probably stuck with it.  Bloody marvellous!

I Can Has Vista Sidebar Gadget?

O hai!

I hart lolcatz.

I hart lolcatz so much dat wun dai I thoughted “I can has lolcatz wen I makes teh pooter turn on?” So I maded a Vista gadjit an now I has new lolcatz every dai.

If u hart lolcatz liek mee an wants lolcatz in ur pooter makin u laff, downlodes mah gadjit. I has tested it 4 liek rly long time. It rly works, srsly. An evry1 needses moar lolcatz.

Kthxbai.

Continue reading “I Can Has Vista Sidebar Gadget?”

ACT – SessionID and Login Problems With ASP .NET 2.0

I recently encountered a problem with Microsoft’s ACT (part of Visual Studio 2003) when testing a web service by emulating a browser-based client. For posterity’s sake, here’s an overview of the problem and, more importantly, the solution.

Background
Using Application Center Test (ACT) to help automate performance testing designed to compare the performance of a web service running on ASP .NET 1.1 with ASP .NET 2.0.

Problem
Originally .NET 2.0 seemed to be performing many times better than .NET 1.1, but it was soon discovered that when running .NET 2.0, ATC was receiving a lot of 302 errors on 2.0 which it wasn’t on 1.0. On further investigation the Web Service wasn’t actually making all the correct database calls and on installing HTTP Monitor, it became apparent that the login wasn’t working. When I recorded the test using Internet Explorer 7, the HTTP requests worked as expected, however when ATC repeated them it was not returning the ASP.NET_SessionID.

Continue reading “ACT – SessionID and Login Problems With ASP .NET 2.0”