My Blog List

Wednesday, December 14, 2011

Quick Way to Disable Adobe Flash on Common Browsers

Sometimes you might want to disable Flash to prevent annoying ads, to improve page speeds,to test your web code  etc. Here are some quick and dirty ways to do it :)

Internet Explorer:

image image

Right-click and say disable.

FireFox:

Press CTRL+SHIFT+A

image

Chrome:

Type about:plugins in the address bar and hit enter

image

Safari:

image Uncheck Enable Plugins.

 

Opera:

Hit F12 and uncheck Enable Plugin

image

Friday, October 21, 2011

jQuery Plugin for Zebra Striping tables including dynamic Row Insertion Deletion

 

This is a simple jQuery plugin which applies alternating CSS for rows in a table.

Offical jQuery site :

http://plugins.jquery.com/project/ZebraStriping

Download (right-click and save as..):

jquery.zebra.js

Features:
1. Alternate row CSS
2. Row hover CSS
3. Handles row insertion/deletion dynamically via javascript(does not work for IE as it does not support DOMNodeInserted event.)
4. Option to exclude header row
5. Works with THEAD and TBODY too.

note: workaround for IE is the zebra plugin must be called again if a new row is added via javascript.

Example usage:

Save the code at the bottom of the page as jquery.zebra.js.

Include it in the HEAD section of your page like:

<script src="jquery-latest.js"></script>
<script src="jquery.zebra.js"></script>

And then call it on a table like:

$("#targetTable,#targetTable2").zebra({
		excludeHeader:true,
		hover:'hoverClass',
		odd:'oddClass',
		even:'evenClass'
	});
 

Plugin Code:

/** 
 * Namespace to avoid variable name collision
 * 
 * Author: Sajjan Sarkar
 * 
 *  */
(function($)
{
	/** Plugin starting point,options is optional but this plugin does nothing if options is null */
	$.fn.zebra = function(options)
	{
		/** Current settings */
		var settings = {
			even : '',/** CSS class name for even rows */
			odd : '',/** CSS class name for odd rows */
			hover : '',/** CSS class name rows when hovered */
			excludeHeader : false /** true to exclude thead/ first row  */
		};
		/** to maintain jQuery chainability */
		return this.each(function()
		{
			/** local variables identifying whether the table has thead/tbody */
			var hasTHead = $("thead", this).length == 0 ? false : true;
			var hasTBody = $("thead", this).length == 0 ? false : true; // not used as of now
			
			/** Variable to store reference to the table as the this keyword in the
			 *  apply() function would point to the DOMWindow object due closure.
			 *  
			 */
			var gTable = this;
			
			/** Store the table header jQuery collection based on whether there is a THEAD , also for performance */
			if (hasTHead)
				var headerSelector = $("thead > tr", this);
			else
				var headerSelector = $("tr:first-child", this);
			/** If options exist, lets merge them with our default settings */
			if (options)
			{
				$.extend(settings, options);
			}
			/** 
			 *  For browsers that support this (IE does not support it), everytime the DOM structure is changed
			 *  we test if a new row is added to the table and apply the plugin to the entire table again
			 *  as the row could have been added in the middle.
			 *  Note: for IE, the zebra plugin must be called again if a new row is added
			 *  */
			$(this).bind('DOMNodeInserted', function(event)
			{
				/** Check if a row has been inserted */
				if (event.relatedNode.nodeName == "TR")
				{
					apply();					
				}
			});
			
			/** internal function to apply the plugin logic. */
			var apply = function()
			{
				/** Apply zebra striping to header row/s */
				if (hasTHead)
				{
					$("thead > tr:even", gTable).removeClass(settings.even + " " + settings.odd).addClass(settings.even);
					$("thead > tr:odd", gTable).removeClass(settings.even + " " + settings.odd).addClass(settings.odd);
				}
				/** Apply zebra striping to body rows */
				$("tbody > tr:even", gTable).removeClass(settings.even + " " + settings.odd).addClass(settings.even);
				$("tbody > tr:odd", gTable).removeClass(settings.even + " " + settings.odd).addClass(settings.odd);
				
				/** Apply hover logic to entire table */
				$("tr", gTable).hover(function()
				{
					$(this).addClass(settings.hover);
				}, function()
				{
					$(this).removeClass(settings.hover);
				});
				/** Exclude header if option is set */
				if (settings.excludeHeader)
				{
					headerSelector.removeClass(settings.even + " " + settings.odd);
					headerSelector.unbind('mouseenter mouseleave');
				}
			};
			/** Call it */
			apply();
			// plugin code here
		});
	};
})(jQuery);

Sample:


image

Wednesday, September 14, 2011

Simple SQL Server Split Function

 

I know there are a lot of solutions to this on the internet, here’s mine!

Sample use-cases:

untitled

 

Code:

 

CREATE FUNCTION [dbo].[Split]
    (
      @list NTEXT ,
      @Delim CHAR(1) = ','
    )
RETURNS @tbl TABLE
    (
      items VARCHAR(4000) NOT NULL
    )
AS 
    BEGIN
        DECLARE @pos INT ,
            @textpos INT ,
            @chunklen SMALLINT ,
            @str NVARCHAR(4000) ,
            @tmpstr NVARCHAR(4000) ,
            @leftover NVARCHAR(4000)
        IF @Delim IS NULL 
            BEGIN
                INSERT  INTO @tbl
                        ( items )
                VALUES  ( CAST(@list AS VARCHAR)  -- items - varchar(4000)
                          )
                RETURN
            END
        SET @textpos = 1
        SET @leftover = ''
        WHILE @textpos <= DATALENGTH(@list) / 2 
            BEGIN
                SET @chunklen = 4000 - DATALENGTH(@leftover) / 2
                SET @tmpstr = LTRIM(@leftover + SUBSTRING(@list, @textpos,
                                                          @chunklen))
                SET @textpos = @textpos + @chunklen
                SET @pos = CHARINDEX(@Delim, @tmpstr)
                WHILE @pos > 0 
                    BEGIN
                        SET @str = SUBSTRING(@tmpstr, 1, @pos - 1)
                        IF LTRIM(RTRIM(@str)) <> '' 
                            INSERT  @tbl
                                    ( items )
                            VALUES  ( @str )
                        SET @tmpstr = LTRIM(SUBSTRING(@tmpstr, @pos + 1,
                                                      LEN(@tmpstr)))
                        SET @pos = CHARINDEX(@Delim, @tmpstr)
                    END
                SET @leftover = @tmpstr
            END
        IF LTRIM(RTRIM(@leftover)) <> '' 
            INSERT  @tbl
                    ( items )
            VALUES  ( @leftover )
        RETURN
    END

Tuesday, April 26, 2011

Searching Stored Procedures Across SQL Server Databases


Here’s a little utility SP I wrote which searches all stored procedures in all databases in your SQL Server Instance for an input string.
In SQL Server Management Studio 2008 there is an option to do filtered searches but it only look inside the selected database. Now honestly I am not sure you should be in a position where you would want to search stored procedures across Databases, but if you do find the need, hope this is useful!
Example:
If you compile this utility into DB1, then simply say :
EXEC DB1..sp_SearchInSPs 'mySearchString'

[UPDATE:]Download Link:



Code:


  1: SET ANSI_NULLS ON
  2: GO
  3: SET QUOTED_IDENTIFIER ON
  4: GO
  5: -- =============================================
  6: -- Author:  Sajjan Sarkar with help of SungUngKim
  7: -- Create date: April 26, 2011
  8: -- Description: stored procedure to search all SPs for a given string in ALL databases. 
  9: -- =============================================
 10: -- Sample to run
 11: --EXEC DB1..sp_SearchInSPs 'BillingOvertimeCalcFactor'
 12: 
 13: ALTER PROCEDURE [dbo].[sp_SearchInSPs] 
 14: (
 15:  -- Add the parameters for the stored procedure here
 16:     @SearchString VARCHAR(100) = ''   
 17: )    
 18: AS 
 19:     BEGIN
 20:  --Declare 
 21:         DECLARE @SString NVARCHAR(50)
 22:         DECLARE @getdbname SYSNAME
 23:         DECLARE @sqlstm NVARCHAR(1000)
 24:         CREATE TABLE #tmp
 25:             (
 26:               DBName VARCHAR(100) ,
 27:               SPName VARCHAR(100)
 28:             )
 29:         DECLARE dbname CURSOR
 30:         FOR
 31:             --get all the names of the Databases in order by name
 32:   SELECT  '[' + name + ']'
 33:             FROM    master.dbo.sysdatabases            
 34:             ORDER BY name 
 35:         OPEN dbname
 36: 
 37:   --Get the first Name
 38:         FETCH NEXT FROM dbname INTO @getdbname
 39: 
 40:         WHILE @@FETCH_STATUS = 0 
 41:             BEGIN
 42:                 PRINT @getdbname
 43:     --set the search string
 44:                 SET @SString = @SearchString
 45: 
 46:     --append the search pattern
 47:                 SET @SString = '%' + @SString + '%'
 48:                 SET @sqlstm = 'SELECT  DISTINCT ''' + @getdbname + ''',
 49:         s.name     
 50:         FROM    ' + @getdbname + '.dbo.syscomments c
 51:         INNER JOIN  ' + @getdbname + '.dbo.sysobjects s
 52:         ON      c.id = s.id
 53:         WHERE   s.type IN ( ''p'', ''tr'' )
 54:         AND c.text LIKE ''%'+@SString+'%''
 55:         ORDER BY [Name]'
 56: 
 57: 
 58:   --Execute the Query
 59:                 INSERT  INTO #tmp
 60:                         EXEC ( @sqlstm
 61:                             )
 62:                 FETCH NEXT FROM dbname INTO @getdbname
 63:             END
 64: 
 65:   --Close the Cursor and Deallocate it from memory
 66:         CLOSE dbname
 67:         DEALLOCATE dbname 
 68:   
 69:         SELECT  *
 70:         FROM    #tmp AS T
 71:         DROP TABLE #tmp
 72: 
 73:     END
 74: