Showing posts with label javascript. Show all posts
Showing posts with label javascript. Show all posts

Wednesday, October 15, 2008

Running javascript after window.onload without breaking Sharepoint

If you're anything like me and are working with a lot of Javascript within Sharepoint. You will eventually need to run a script after the page loads. So you could add defer="defer" to your script tag, which sometimes works and sometimes doesn't.

You might be tempted to window.onload = someFunction(). But this will break the way Sharepoint load's it's pages. As Sharepoint also needs to do a lot of things after the page loads.

So the solution is:
window.attachEvent("onload", someFunction);

So instead of overriding the onload event. You are now adding an event to the queue. Simply replace the "someFunction" bit with your function name.

Sunday, June 15, 2008

Embedding a resource in your ASP.NET 2.0 assembly

While developing a web part for Sharepoint I came across an issue where I needed to have Javascript manipulate elements in the web part. I searched around for the best way to include my Javascript file as part of my Sharepoint solution and came across many articles but I couldn't get their methods to work until I came across this one:
http://www.codeproject.com/KB/aspnet/MyWebResourceProj.aspx

I'm not sure why but "Page.ClientScript.GetWebResourceUrl" seems to work for me but "Page.ClientScript.RegisterClientScriptResource" as per this article doesn't.

Wednesday, June 11, 2008

Dynamically set the onclick attribute of a html element using Javascript

I've been trying to dynamically set the onlick attribute of a span element in Javascript.
I tried a couple of things including:
var newNavNode = document.createElement("span");
newNavNode.onclick = "horizontalNavClick('"+nodeValue+"');" ;

But this wasn't working as a reference to the function was needed and not a string.

I googled and found this post: http://codingforums.com/archive/index.php?t-55356.html

And modified my script to:
var newNavNode = document.createElement("span");
newNavNode.onclick = new Function("horizontalNavClick('"+nodeValue+"')");

This worked like a charm. Thanks Willy Duitt!

Happy scripting everyone!