mikeobrien.net Curriculum Vitae Blog Labs
Wednesday, October 21, 2009

I just changed the domain of my site and I wanted to create a page notifying that the site has moved and then auto redirect them after a few seconds. Here’s how I did it:

1) I’m using IIS 7.5 so I opted to use the IIS7 RewriteModule. I simply ran the installer and the applet appears under the site options.

2) I created a catch all site in IIS and bound it to all the old public domains:

image

3) Then created a UrlRewrite Rule to rewrite all requests to /Default.aspx (Except ones specifically to /Default.aspx):

image

4) Next created the redirection page (Stripped down for clarity):

<%@ Page Language="C#" %>
<%
string redirectUrl = string.Format("{0}://{1}{2}{3}", 
    Request.Url.Scheme,
    Request.Url.Host.Replace("mikeobrien.net", "mikeobrien.net"),  
    (Request.Url.Port != 80 ? ":" + Request.Url.Port : string.Empty), 
    Request.Headers["X-Original-URL"]);
%>
<html>
<head>
    <title>We've Moved</title>
    <script language="javascript">
        function RedirectPage(url, seconds)
        {
            self.setTimeout('self.location.href = \'' + url + '\';', seconds * 1000);
        }

        function CountDown(seconds, elementId)
        {
            if (seconds == 0) return;
            document.getElementById(elementId).innerHTML = seconds;
            self.setTimeout('CountDown(' + (seconds - 1) + ', \'' + elementId + '\');', 1000);
        }
    </script>
</head>
<body onLoad="RedirectPage('<% = redirectUrl %>', 5);CountDown(5, 'timeLeft');">
    <h3>We've moved!</h3>

    <p>
    The page you requested can now be found <a href="<% = redirectUrl %>">here</a>. 
    You will be redirected in <span id="timeLeft">5</span> seconds.
    </p>
</body>
</html>

5) And voila!

image

C# | IIS 7 | IIS7.5 | JavaScript
Wednesday, October 21, 2009 1:23:22 AM (GMT Daylight Time, UTC+01:00)  #   |  Comments [1]  |  Trackback
Tuesday, December 18, 2007

The following JavaScript class enables you to get the expanded coefficients of a binomial raised to a particular power. Pass in the power and it will return an array of coefficients. For example, passing in a power of 6 returns {1, 6, 15, 20, 15, 6, 1}. This is also a row in Pascal's Triangle. It's using the following formula to calculate the coefficients, where n is the the power the binomial is raised to and k is the term position (0 to n).

Here is the class (PascalsTriangle.js):

function PascalsTriangle()
{
    this.GetExpandedBinomialCoefficients = function(power)
    {
        var coefficients = new Array(power);

        for (var index = 0; index <= power; index++)
        {
            coefficients[index] = this.GetExpandedBinomialCoefficient(power, index);
        }

        return coefficients;
    }

    this.GetExpandedBinomialCoefficient = function(power, term)
    {
        return this.GetFactorial(power) / (this.GetFactorial(term) * this.GetFactorial(power - term));
    }

    this.GetFactorial = function(value)
    {
        if (value <= 1)
            return 1;
        else
            return value * this.GetFactorial(value - 1);
    }
}

The class can be used as follows:

var pascalsTriangle = new PascalsTriangle();
var coefficients = pascalsTriangle.GetExpandedBinomialCoefficients(6);
alert(coefficients.join(' '));

This video covers Pascal's Triangle...

Tuesday, December 18, 2007 3:03:17 PM (GMT Standard Time, UTC+00:00)  #   |  Comments [0]  |  Trackback
Creative Commons License