Javascript Detect Null Object

There are many methods to check if an object is null in javascript, this post gives most of the options that can used to detect this. http://lists.evolt.org/archive/Week-of-Mon-20050214/169524.html

Well, first of all, in JavaScript null is an object. There’s anothervalue for things that don’t exist, undefined. The DOM returns null foralmost all cases where it fails to find some structure in thedocument, but in JavaScript itself undefined is the value used.

Second, no, they are not directly equivalent.If you really want to check for null, do:

if (null == yourvar) // with casting
if (null === yourvar) // without casting

If you want to check if a variable exist

if (typeof yourvar != ‘undefined’) // Any scope
if (window['varname'] != undefined) // Global scope
if (window['varname'] != void 0) // Old browsers

If you know the variable exists but don’t know if there’s any valuestored in it:

if (undefined != yourvar)
if (void 0 != yourvar) // for older browsers

If you want to know if a member exists independent of whether it hasbeen assigned a value or not:

if (’membername’ in object) // With inheritance
if (object.hasOwnProperty(’membername’)) // Without inheritance

If you want to to know whether a variable autocasts to true:
if(variablename)

Client and Serverside Button Click in ASP.Net

While I was going through the comments in this post here, someone asked a question around how button click function worked, so I thought I would write a post around this topic.

There are two types of click that can be associated with a button, one is a server side click function, i.e in a function in VB code that gets called, and there is a javascript click function, a function that gets called in javascript when a button is clicked on.
Javascript onclick function gets executed before the server side on click function.
Here are the methods of specifying both.

Javascript click function

1. in asp, specify the javascript to be called using the onclientclick attribute, what ever is specified in this attribute will be executed on the client side.


2. In the code behind, specify the javascritp to be called using the onclick attribute.
button.Attributes.add(”onclick”,”alert(’me’);”)

3. Using a library like JQuery, the onclick function can be specified like so
$(document).ready(function(){$(’#button’).click(function() {alert(’me’);});});
If you are doing some validation in the javascript and only want the server side click function to execute if the javascript validation is successful then you can return false in the javascript function called. If false is returned in the javascript function, the server side event is not executed.

Serverside Click Function

1. In the design view of the ASP, double click on a button, and a function similar to this should be generated


Private Sub
button_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles button.Click
End Sub

notice that the Handles button.click part, this tells that code that this function is used to handle the click event of the button

2. In the source view, there are 2 drop down lists at the top, the left one allows selecting of the different form elements on the page, the right hand side allows viewing of the the functions available for each element. In the left, select the button, and then on the right, select Click, a sub similar to above should be created.

3. You can also specify the click function in the ASP and not have the Handles button.click part in the code.


Private Sub
button_Click(ByVal sender As Object, ByVal e As System.EventArgs)
End Sub

This would only work if the parameters specified matches the ones required for the button click event.

SQL statement to search inside stored procedure

Useful script for searching in stored procedures.

SELECT OBJECT_NAME(id)FROM syscommentsWHERE [text] LIKE ‘%searchtext%’AND OBJECTPROPERTY(id, ‘IsProcedure’) = 1GROUP BY OBJECT_NAME(id)

Ajax Dotnet AutoComplete Clear Cache

With the .net auto complete control, there is an attribute enablecache which tells the auto-complete to use caching or not. If this attribute is set to true, typing the same search text within a set number of seconds, instead of retrieving the data again from the database, the previous search results will be used.

Although caching is good in most cases, there are some cases where we need to clear the cache. For example, we have an auto-complete text-box, under this there are 2 check-boxes. Checking the check-box will change the search results. On clicking of the check-box we need to clear the cache of the auto-complete, so even though the same search text is enter, we need to do a complete new search again.

To do this, we can use the following java-script code on clicking of the check-box.

var autocompltebehaviour = $find(’autosearch’);
autocompletebehaviour._cache = null;

The id used in the $find function is the behaviour id attribute of the autocomplete textbox.
Using this method you can have both the caching of the autocomplete and no caching working at the same time.

Using Ajax for Web Application Development

Ajax web programming enables a web application development team to create a sit that allows users to perform certain functions without the need for redrawing or reloading an entire screen.

Benefits:

1. It offers site developers an extra level of innovation that can make a site easier to use for all visitors.

2. When implemented correctly, it gives users the feeling that a web application is more responsive than a traditional web site.

3. It can allow a business to give its site new functionality and new services to be used by partners or clients.

4. It can give parity to a business.

Things to be careful:

1. This type of web programming can make a site more difficult for visitors to use.The web is at its core a very user-friendly system, but Ajax can introduce new behaviors that people may not be ready for or may be confused by.This can cause visitors to leave your site quickly, without actually performing the tasks.

2. Ajax programming can create a host of other problems with which businesses should be concerned.

3. Ajax can open up a webserver to have an increased attack surface. This form of web programming unfortunately gives a hacker more ways to get into the server than there were before.The solution to this is to make sure that the web application development team is more vigilant about security and testing, and to constantly keep watch on the activity occurring on the site.

4. Different browsers read Ajax differently, the web application development team will have to do additional testing for accessibility.