Monday, December 22, 2008

Protect Javascript

Sometimes you may have proprietary functions/algorithms that you don't want copied as they are a core object to what you're offering on your Web site (i.e. Users have payed a subscription fee to use custom-made algorithms). You want to prevent people from saving the Web site to their hard drive and simply using the functions there. Granted, you could protect the function in the code behind, or on a postback in the ASP/PHP programming, but sometimes the speed of client-side scripting is a determining factor. The downside is anyone can view the JavaScript (even if it's referenced in an external .js file). Here's a simple trick:

Note: This trick was originally done in the PHP environment; the concept should translate into the .NET world:

In your main .html, .php, .asp page, point to a file that will contain your javascript code. The file extension should be .php or .asp (whichever environment you're in):
<script language="javascript" type="text/javascript" src="/protectedJavascript.php"></script>

In the "protectedJavascript.php" file, place the following code:

if(!eregi("/originatingPage.html",$_SERVER['HTTP_REFERER'])) {
   echo "Message to those snooping here.";
} else {
   echo "//JAVASCRIPT CODE HERE";
}

Where "/originatingPage.html" is the path of which calls this protected page. In the PHP world. the !eregi() function simply looks for a match between the two strings passed in.

Anyone trying to access this page will be shown the "Message to those snooping here." text (or whatever you have placed here.) because their HTTP_REFERER will not (or ever) be "/originatingPage.html". The only way they could see just the code is if you had a hyperlink pointing directly to "protectedJavascript.php" from "/originatingPage.html."

You can obtain this same functionality with an .asp page.

Validate Decimals with Javascript

Validating a decimal such as a measurement of time or currency (10.5 or 0.4) is as easy as:

function valNum(inNum {
   var RegEx = /[0-9]{1,8}\.?[0-9]{0,2}/;
   var ValidNumber = inNum.match(RegEx);
   return ValidNumber;
}

This function will return the correct number. If you feed in "10.5foo" the function will spit back 10.5. If you feed in "bar" the function will return nothing.

Tuesday, November 18, 2008

Automatically Scroll to the bottom of a DataGridView

DataGridView.CurrentCell = DataGridView(1, DataGridView.RowCount - 1)


Where DataGridView is the instance name of your DataGridView component. You're passing in Column and Cell selection into DataGridView()

Friday, October 24, 2008

Generate a Random String

If you need to generate a random string of mixed characters, this simple function will do it for you:
Public Function GenerateRandomString() As String
   GenerateRandomString= System.Guid.NewGuid.ToString
End Function

This will return something like this: 80a7a705-9c23-47bc-92b6-2b5c4f9d728d. Which you then can do various string functions to make it the way you want.

Thursday, October 23, 2008

Formatting Dates in GridView

If you have a date coming back to your GridView and it displays "11/01/2008 12:00am" and you'd rather just display the date and not the time, you can fill in the DataFormatString field when editing the column, with: {0:MM/dd/yyyy}

Friday, August 22, 2008

Call a Function in your MasterPage from your .aspx page

If you're going to be using a common function throughout the Web site, you can include the function in your MasterPage and gain access to it from each aspx that inherits from this MasterPage; like so:

CType(Me.Master, MasterPages_MasterPageName).FunctionOrSubName()


Simple as that!

Thursday, August 14, 2008

Randomize Query in SQL

Sometimes you want to return a bunch of data from a table, but you want it coming back in a random order. Maybe it's for keeping some content on a home page fresh. Here's how:

ssql="SELECT TOP 10 * FROM [someTable] ORDER BY NEWID()"


The key here is "ORDER BY NEWID()"