Friday 28 May 2010

AddThis validation issue

The AddThis button is a great addition to any website, however if your site needs to conform to W3C standards you may need to tweak the default HTML snippet that is provided, the issue is the use of the # in one of the JavaScript references:


<script type="text/javascript" src="http://s7.addthis.com/js/250/addthis_widget.js#username=blahblah">
</script>


Simply change the # to a ? and you should be validating fine:


<script type="text/javascript" src="http://s7.addthis.com/js/250/addthis_widget.js?username=blahblah"></script>

Monday 17 May 2010

Linq equivalent of for-each

If you've ever wondered about using Linq for a more elegant implementation of a for each loop then this may be useful, use the Select<> method to perform actions against a set of items, as long as you return a value of any sort then you can perform any action during the selection.

In the example below I need to remove all folders that were written to over an hour ago, it also returns info on the folders processed and whether they were successfully removed:


if (string.IsNullOrEmpty(RootWorkingFolder) || !Directory.Exists(RootWorkingFolder)) return;

Directory.GetDirectories(RootWorkingFolder)
.Where(f => Directory.GetLastWriteTime(f) < DateTime.Now.AddHours(-1))
.Select(delegate(string f)
{
try
{
Directory.Delete(f, true);
return new {Folder = f, Removed = true};
}
catch (IOException)
{
return new {Folder = f, Removed = false};
}
}).ToList();


The ToList method is to ensure that the query is executed immediately.

Pretty useful to know :-)

Wednesday 5 May 2010

Visual Studio 2010 running slow

For anyone suffering with the performance of Visual Studio 2010, particularly intellisense, Microsoft has published a support article on the matter, apparently an update to Windows Automation API 3.0 can be applied that will improve matters.

The article is here: http://support.microsoft.com/kb/981741/.

Thursday 29 April 2010

Assigning click event using jQuery in FireFox not working

I've just stumbled upon a strange problem which highlighted some badly formatted Javascript on my part, I had around 30 anchors on my page which needed to have Click event handlers assigned in order for me to override the href invocation, the reason for this was to prevent my Click from propagating through the underlying DIV element that the anchor was sitting in, so here was my original code:


$(document).ready(function() {
$(".addthis_button_facebook, .addthis_button_twitter").click(function() {
return click_addthis(event, this.href)
});
});


Which actually worked fine in every browser except FireFox, this was stumping me for ages until I decided to post a question at the wonderful Stack Overflow (what a god-send that place is!), which resulted in someone immediately spotting the missing argument in my function event.

The code should've been:


$(document).ready(function() {
$(".addthis_button_facebook, .addthis_button_twitter").click(function(e) {
return click_addthis(e, this.href)
});
});


If you haven't noticed it, it is the argument in the function that is required, this can then be passed onto the function being called.

I hope this helps someone who may have a similar issue/oversight, although it's probably just me and my limited JS experience!

So thanks again to the helpful chaps at stack overflow!