.relativelypositioned

Category: Javascript

Cancel window.location.href = ‘some url’;

Did some googling on this and didn’t really find any great solutions for canceling setting the window’s location. Say for example you have a grid whose datasource is filtered by parameters in the querystring and someone clicks “Show All” or forgets to filter the results; this may take a very long time to load and the user probably doesn’t want to wait and wants to cancel.

After playing with a couple different ideas the best I found to accomplish this was to set the locations href to itself on your cancel buttons click event, see below.  I put it in a setTimeout because it will get hung up on the current thread that is trying to load the slow page.

onclick=”window.setTimeout(function() { window.location.href=window.location.href; }, 10);”

By reseting the window location’s href you cancel the event and reload to your current page. I’ve tested on Chrome, FF and IE, all seem to handle it quite well.

If anyone has a better way of doing this, i’m all ears!

Javascript Trim String

You can find this or something similar nearly anywhere by simply googling it, the only reason why i’m blogging this is so I don’t have to google it. So, here it is, the javascript trim string function.

function TrimString(str) {
return str.replace(/^\s+|\s+$/g, '');
}

.matti

Get Element Position

I needed a quick and easy function to get an element’s position in javascript today so I wrote a function that returns the top and left coordinates. I’ve tested this in IE 6/7, FF3+ and Chrome.

function getPosition(element) {
var top = left = 0;
while (element) {
top += element.offsetTop;
left += element.offsetLeft;
element = element.offsetParent;
}
return [top, left];
}

.matti

Follow

Get every new post delivered to your Inbox.