Thursday, September 25, 2008

Calculating absolute position

// here is a Javascript routine that calculates the absolute position of an html
// element by recursively traversing the parent dom elements
// (works in IE, Firefox, Safari , etc.)

Map.Drawing.Point.prototype.addAbsolutePosition = function(obj) {
var X, Y;
var cur;
if (!obj) {
return this;
}
else
{
X=parseInt("0" + this.x);
Y=parseInt("0" + this.y);
var o = obj;
if (o.offsetParent) //then this object has a container that affects its position
{
while (o.offsetParent)
{
X += parseInt(o.offsetLeft);
Y += parseInt(o.offsetTop);
o = o.offsetParent; //traverse the hierarchy upward
}
}
else if (o.x || o.y) //no container but an absolute position
{
if (o.x) X += parseInt(o.x);
if (o.y) Y += parseInt(o.y);
}

this.x = X;
this.y = Y;

return this;
}
};

Tuesday, September 16, 2008

301 Redirects

To fully retire a page and redirect to a new page SEO (Search Engine Optimization) recommends 301 redirects (as opposed to the regular 303 redirects).

Below is the essential code fragment in Global.asax of the .NET framework to issue a 301 SEO friendly redirect:

// below we are redirecting to www.mynewurl.com
protected void Application_BeginRequest(Object sender, EventArgs e)
{
string oldserver = HttpContext.Current.Request.Url.AbsoluteUri;


// Check whether it is an old url
if (oldserver.IndexOf("myoldurl.com") > -1)
{
HttpContext incoming = HttpContext.Current;

incoming.Response.StatusCode = 301;
incoming.Response.AddHeader("Location", "http://www.mynewurl.com"));
incoming.Response.End();
return;
}

}

// end of 301 redirect logic

In .NET this could better be done with an httpModule. Coding this as an httpModule (instead of in Global.asax) is beyond the scope of this article.