Append JavaScript Tag Dynamically

Sometimes you need to append a specific JavaScript file based on the user’s browser or as a result of some equation. Here is a little handy function that will help you do this.

function appendScriptTag(src) {
       var jstag = document.createElement('script');
       jstag.setAttribute('type', 'text/javascript');
       jstag.setAttribute('src', src);
       document.getElementsByTagName("head")[0].appendChild(jstag);
}

Trim Prototype

Some legacy browsers such as IE8 do not natively support the trim() function in JavaScript. Though newer browsers do one cannot expect the majority of web site visitors to use them, yet. Because of this I found out how to make a trim function in JavaScript that seems native, in use anyways.

// Set it
String.prototype.trim = function() {
    return this.replace(/^\s+|\s+$/g, '');
};

// Use it
var name = "     Cesar      ";
if (name.trim().length == 0) {
    alert('Invalid name.');
} else {
    alert('Valid name.');
}

Applying Simple Ajax Functionality On Visual Studio

Here are the simple steps to applying ajax functionality to an aspx page using the UpdatePanel control.

  1. Drag and drop a ScriptManager item from the Ajax Extensions section on the toolbox onto the form you wish to add Ajax to.
  2. Drag and drop an UpdatePanel from the Ajax Extensions section on the toolbox onto the form you wish to add Ajax to.
  3. Drag and drop your form controls to the inside of the UpdatePanel.

It’s as simple as that. Here is what the code should look like on the Default.aspx page.

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    <div>
        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
            <ContentTemplate>
                <asp:Label ID="lblMessage" runat="server" Text="Label"></asp:Label>
                <br />
                <asp:TextBox ID="txtMessage" runat="server"></asp:TextBox>
                <asp:Button ID="btnSubmit" runat="server" Text="Submit" />
            </ContentTemplate>
        </asp:UpdatePanel>
    </div>
    </form>
</body>
</html>

And here is what the code-behind page (Default.aspx.vb) should look like. Keep in mind this is a very simple example.

Partial Class _Default
    Inherits System.Web.UI.Page
    Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
        Me.lblMessage.Text = Me.txtMessage.Text
    End Sub
End Class

Creating a Multi-dimension Array

In my experience with other programming languages making a multi-dimensional array is usually a simple thing. In JavaScript however it is slightly different (not necessarily difficult). Though there are a few other ways to create a multi-dimensional array, some of them involving loops, which I refuse to submit to, I think this is the easiest, most comprehensible way.

	var myArray = new Array(
		new Array(), // array myArray[0][X]
		new Array(), // array myArray[1][X]
		new Array(), // array myArray[2][X]
		new Array()  // array myArray[3][X]
	);