Entries for month: April 2010

Clear values of all form elements using jQuery

Posted by Akash Bavlecha on April 29, 2010
  0 comments

Hello friends,

I always wanted to have one common function to reset all form elements. Please have a look at jQuery code that does the same thing. It finds out all element of form and loops over it and reset them. I hope this thing will be helpful specifically where there are many search fields and you need to reset them frequently.

function resetFormElements(frm){
$(frm).find(':input').each(function() {
    switch(this.type){
        case 'password':
        case 'select-multiple':
        case 'select-one':
        case 'text':
        case 'textarea':
            $(this).val('');
            break;
        case 'checkbox':
        case 'radio':
            this.checked = false;
    }
});
 }

Note : I noticed that some time in IE selectbox is not getting cleared. So you can modify script and put

'$(this).attr("selectedIndex",0);'
for 'select-one' case. It will work in both IE and mozilla.

 

Demo

 




List all databases, objects, tables by a simple single line query.

Posted by Akash Bavlecha on April 27, 2010
  0 comments

Hello,

Try below query and see all databases and objects get listed in sql server.

sp_msforeachdb 'select "?" AS db, * from [?].sys.tables';




Correct Method to check if a Javascript function is Exists or not

Posted by Nirav Patel on April 27, 2010
  0 comments

Hi everyone,

Sometime you might want to call a function in Javascript but check whether the function exists or not before calling it. You can test if a function exists in Javascript by simply testing for the name of it in an if() condition.

But Note that you can't test for the function name by itself.
This will work but if the function doesn't exist the Javascript will error out.

if(some_function_name)
		{}

The Correct method is Like this

 if(window.some_function_name)
		{} 
 

It's a method of the window object so you need to test for window.some_function_name like so, where some_function_name is the name of the function you wish to test for.




Visitor's Browser Window Display Area

Posted by Rahul on April 24, 2010
  0 comments

To know the available display area in a visitors browser excluding the pixels occupied by the toolbars and other add-ons on the users browser use the following javascript code.

<script type="text/javascript">
     availArea_width = screen.availWidth;
     availArea_height = screen.availHeight;
</script>

* Screen.Width and Screen.Height will return the resolution of the users monitor.




ColdFusion And AJAX File Upload

Posted by Naresh Patel on April 24, 2010
  0 comments

Hello All,

I have created simple jquery plugin for file upload.
I have used multiple file upload in Ajax but it’s not possible to upload file in Ajax.
This problem can be solved by this two method.

  1. flash to solve this problem.
  2. JavaScript works.

Javascript works more nice than Flash.




Overwrite old click event function Not Working in IE

Posted by Nirav Patel on April 23, 2010
  0 comments

Hi everyone
    I just came across this problem that is happening in IE.
    In the html I am adding an onclick event to an element that calls a function 'changeStatus' like this :

<a id="my_a" onclick="changestatus('my_a',1);" href="javascript:void(0);">Click Here</a>



function changestatus(id,status)
    {
        alert(status);
        jQuery('#'+id).html('Yes');
        jQuery('#'+id).unbind('click').removeAttr('onClick');
        jQuery('#'+id).click(function(event){changestatus(id,0);});
    }


Inside the changeStatus function I added the .unbind('click').removeAttr('onClick'); in order to remove the old onclick event and then add the new one.

This is working fine in FF, but in IE it is calling both the new click event and the old at the same time for some reason. I think that this causes jQuery to not know what events are taking place elsewhere, so both events get run.
When I put the following code and just bind the click event with jquery on document.ready and not set the onclick attribute of the anchor tag.

 

<a id="my_a" href="javascript:void(0);" >Click Here</a>

jQuery(document).ready(function(){
        $('#my_a').click(function() {
            changestatus('my_a',1);
        });
    });

This is works well on both FF and IE and I believe that this is the best way to fix this problem.
    Please correct me if this is wrong logic or any other way to fix this problem.




Facing nested param serialization problem - Appending "[]" after variable - jQuery.

Posted by Akash Bavlecha on April 23, 2010
  1 comments

Our current application has been affected by a change in the latest jQuery (1.4.1) from the previous release.

In form submission using ajax, jQuery now posts multiple selectbox or checkboxes values with a character "[]" appended to variable (I think jQuery is being more favorable to PHP).

I used firebug to look my argument passed. Just look below how it passed an argument with "[]" appended.

It is just because jQuery 1.4 adds support for nested param serialization in jQuery.param, using the approach popularized by PHP, and supported by Ruby on Rails.

For instance, {foo: ["bar", "baz"]} will be serialized as "foo[]=bar&foo[]=baz".

If its added for PHP or Ruby on Rails support then what about others existing application? I think jQuery should give an optional boolean param (language specific) for them to set up these settings.

If you need the old behavior, you can turn it back on by setting the traditional Ajax setting.



jQuery.ajaxSettings.traditional = true;





Find longest executing queries in sql server (2005,2008)

Posted by Akash Bavlecha on April 22, 2010
  0 comments

Hi guys,

I have been working with queries today and wanted to find some queries which was very high on its execution time. There was a need in the project to find out which query is running longest in sql server to avoid time outs and to optimize those queries for better performance. See the script below and definately it will be really helpful to you for better performing backend.

 

DBCC FREEPROCCACHE
SELECT DISTINCT TOP 10
t.TEXT QueryName,
s.execution_count AS ExecutionCount,
s.max_elapsed_time AS MaxElapsedTime,
ISNULL(s.total_elapsed_time / s.execution_count, 0) AS AvgElapsedTime,
s.creation_time AS LogCreatedOn,
ISNULL(s.execution_count / DATEDIFF(s, s.creation_time, GETDATE()), 0) AS FrequencyPerSec
FROM sys.dm_exec_query_stats s
CROSS APPLY sys.dm_exec_sql_text( s.sql_handle ) t
ORDER BY
s.max_elapsed_time DESC
GO

Reference : Pinal Dave's Blog




mssql - copy data from one table to another existing table

Posted by Bharat Patel on April 21, 2010
  0 comments

Hello friends,

Today, there was a need  to insert data from one table to another table. There are many ways to insert data from one to another. Sql server provides a functionality to copy data from one to another using SELECT clause also. I hope it may be helpful for you.

Syntax



insert into <table name>
select <field list> from <table name from copy data>


You can insert selected field also.



insert into <table name> (field list)
select <field list> from <table name from copy data>


Example



insert into table2
select * from table1

insert into table2 (no,name,city)
select no,name,city from table1





cfquery using cfscript

Posted by Bharat Patel on April 19, 2010
  0 comments

Hello Friends,

Used to execute a query passing SQL statements to a data source using CFScript. We can execute sql statement using execute method with query object.

How to define query object in cfscript:

<cfscript>
queryObj = new Query();
<!--- 
OR 
--->
queryObj = 
createObject("component","query");
</cfscript>