Entries for month: April 2010

Clear values of all form elements using jQuery

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.

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

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

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

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.

Read more...

Overwrite old click event function Not Working in IE

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.

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)

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

Design by Mark Aplet | Powered by Mango Blog