Open all external links in new window or tab using jquery

In this programming tutorial we will learn how to open all external links in new window or tab using jquery. External links should be open in new window or tab because we should not lost the focus of user from our website if he clicks the external link. We all know that to open a link in a new window, we have to set the target attribute as '_blank' in our HTML link tag. But this process is very time consuming if we have huge number of external links in our page. To get rid of this problem just have a look over following code snippet.

Open all external links in new window using jquery
Put this script just before <body> tag
<script language="javascript" type="text/javascript">
$("a[href*='http://']:not([href*='"+location.hostname.replace("www.","")+"'])").each(function() {
$(this).attr('target', "_blank");
    });
</script>
That's it. And if you want to add css class to external hyperlinks then use this code
<script language="javascript" type="text/javascript">
$("a[href*='http://']:not([href*='"+location.hostname.replace("www.","")+"'])").each(function() {
$(this).addClass('externalLinkClass').attr('target', "_blank");
    });
</script>

And here is the css class
.externalLinkClass {
    color:red;
    font-weight:bold;
    text-decoration:none;
}

So that's it.
I Love your feedback.

Read more...

Check if there is an attribute on an element using jquery

In this programming tutorial we will learn how to check if there is an attribute on an element or not using jquery. We will use attr() function of jquery, yeah jquery because it is very powerful for performing operations over DOM. So let's have a look over code snippet given below in which we will check whether txtName textbox contains name attribute or not.

var attrExists = $("#txtName").attr("name");

// In some browsers, "attrExists" is undefined; for others,  "attrExists" is false. 
// Check for both.
if (typeof attrExists!== 'undefined' && attrExists !== false) {

    // Attribute Exists
}

So that' it.
I love your feedback.




Read more...

Check if an element contains attribute or not using jquery

In this programming tutorial we will learn how to check if an element contains attribute or not using jquery. Using built-in jquery's function we can check if an element contains particular attribute or not but we cannot check if an element contains at least one attribute or not. So let's start

Check if an element contains attribute or not using jquery
function hasAtleastOneAttribute(selector) {
    var attributeExists = false;
    $(selector).each(function(index, element) {
        if (element.attributes.length > 0) {
            attributeExists = true;
            return false; // breaks out of the each once we find any attribute
        }
    });
    return attributeExists;
}

Let's have a look over how to call it.
if (hasAtleastOneAttribute('.myClass')) {
    // Write your stuff here
}

So that's it.
I love your feedback.
Read more...

Sort varchar date in ms sql server

In this programming tutorial we will learn how to sort the varchar date in ms sql server. I face the sorting problem of varchar date couple of days ago and then I decided to write a tutorial in it. I have a column registered_date of datetime datatype in users table; in that column the values are stored definitely in this format 2011-07-21 00:00:00.000. I wanted to get the distinct date in mm/dd/yyyy format and also wanted to sort the date(the purpose of this tutorial) according to my need.

I have easily converted the registered_date column of datetime into varchar and display date in mm/dd/yyyy format by using convert(varchar(10),registered_date,101) function but I failed to sort the date, also I failed to get the distinct date, like illustrated in following picture.
sort varchar date in ms sql server


So for fixation of this problem, I have written following query

Learn How to sort varchar date in ms sql server
Select Distinct convert(datetime,convert(varchar(10),registered_date,101)),   
convert(varchar, registered_date,101) As registered_date
FROM users Where Deleted ='false' order by convert(datetime,convert(varchar(10), registered_date,101)) desc

Output will be:-
2011-07-21 00:00:00.000      07/21/2011
2011-07-19 00:00:00.000      07/19/2011
2011-07-18 00:00:00.000      07/18/2011
2011-07-16 00:00:00.000      07/16/2011
2011-07-13 00:00:00.000      07/13/2011
2011-07-12 00:00:00.000      07/12/2011
2011-07-08 00:00:00.000      07/08/2011
2011-05-03 00:00:00.000      05/03/2011
2011-04-28 00:00:00.000      04/28/2011
2011-04-27 00:00:00.000      04/27/2011
2011-04-14 00:00:00.000      04/14/2011
2011-04-07 00:00:00.000      04/07/2011
2011-04-01 00:00:00.000      04/01/2011
2011-03-29 00:00:00.000      03/29/2011

The query is self explanatory; I have converted the registered_date column into varchar and then again into datetime to get the distinct date and this first column is very important in this scenario which look extra in first sight because using this first extra column i was able to sort the date :).

So that’s it.
I Love your feedback


Read more...

The .whatever address is coming soon to the Web

This summary is not available. Please click here to view the post. Read more...

Get ip address in asp.net using c#

In this programming tutorial we will learn how to get ip address in asp.net with c#. I have already written same tutorial for php. In asp.net it is also very easy to find ip address no matter whether user is behind the proxy or not, you can easily track the user in asp.net. So let's have a look over the following code snippet.
How to get ip address in asp.net using c#
//To get the ip address of the machine and not the proxy use the following code     
            string strIPAddress = Request.UserHostAddress.ToString();
            strIPAddress = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];

            if (strIPAddress == null || strIPAddress == "")
            {
                strIPAddress = Request.ServerVariables["REMOTE_ADDR"].ToString();
            }

So that's it.
I love your feedback.

Read more...

Install PHP on Windows 7 with IIS 7 via FastCGI

In this programming tutorial, we will learn how to install and configure PHP on windows 7 with IIS 7 via FastCGI. I have already written a tutorial about Installing php on windows but now this tutorial is not handy after release of php 5.3 version because php5isapi.dll is no longer available in the php releases starting from 5.3. So my recommendation is with the world’s recommendation and that is to use "FastCGI". Please consider following points before installing PHP on Windows 7.
  1. First of all check the system type; either it is 32 bit or 64 bit. X86 means 32 bit. X64 means 64 bit. Download the appropriate php version according to your system type.
  2. If you are using PHP with IIS you should use the VC9 versions of PHP.
  3. A Thread Safe version should be used if you install PHP as an Apache module. The Non Thread Safe version should be used if you install PHP as a CGI binary.
  4. Non-thread-safe build of PHP is recommended for PHP 5.3 version when using IIS. The non-thread-safe builds are available at » PHP for Windows: Binaries and Sources Releases.

Note: 

VC9 Versions are compiled with the Visual Studio 2008 compiler and have improvements in performance and stability. The VC9 versions require you to have the » Microsoft 2008 C++ Runtime (x86) or the » Microsoft 2008 C++ Runtime (x64) installed. If you have visual studio 2008 install then you don't have to install Microsoft 2008 C++ Runtime.
 
What is FastCGI?
FastCGI is a standard protocol that allows the CGI executable files for application frameworks to interface with the Web server. It is different to the standard CGI protocol in that FastCGI reuses CGI processes for multiple requests, which provides a significant performance boost as compared to CGI. Support for IIS FastCGI enables IIS to host normal CGI programs like PHP or Ruby on Rails by using the FastCGI protocol, and to offer high performance and stability for production deployment of such application frameworks.

For having the IIS FastCGI support, you must have following things:
  • IIS Web server
  • IIS FastCGI extension
  • CGI program (such as php-cgi.exe)
The Web server dispatches HTTP requests to your application to the FastCGI component, which in turn launches the CGI program executable, and forwards the request for processing. Once the request is finished and the response is returned back to the server and sent to the client, the CGI process is reused for a subsequent request. This helps to avoid the high performance penalty of starting a new process for each request, which results in better performance and scalability in a production environment. Now let’s come to the topic.

Install PHP on Windows 7 with IIS 7 via FastCGI

First of all, definitely you need to have PHP. Go to the PHP download page and get the latest non-thread-safe ZIP archive of windows binaries for Windows. As of writing this, the package is called "PHP 5.3 (5.3.6) Non-thread-safe zip package". Unzip the archive to a folder on your hard disk. I use C:\php5.

In the latest version of php, you will not find any php.ini-recommended file in php directory. php.ini-recommended is replaced by php.ini-development and php.ini-production files. php.ini-production is used for production systems; for development systems you should use php.ini-development.

Open the php.ini-development and save as php.ini in the same folder and then use the following configuration setting.

Uncomment cgi.force_redirect by removing preceding; and then set it as cgi.force_redirect = 0

This directive is required for running under IIS. It is a directory security facility required by many other web servers. However, enabling it under IIS will cause the PHP engine to fail on Windows.
It is the minimum setting you need to change so that PHP can work with IIS.

Additionally, you may want to use
Uncomment cgi.fix_pathinfo by removing preceding; and then set it as cgi.fix_pathinfo = 1

This lets PHP access real path info following the CGI Spec. The IIS FastCGI implementation needs this set.

Uncomment fastcgi.impersonate by removing preceding; and then set it as fastcgi.impersonate = 1

FastCGI under IIS supports the ability to impersonate security tokens of the calling client. This allows IIS to define the security context that the request runs under.

Uncomment extension_dir by removing preceding; and then set it as extension_dir=C:\php5\ext.

The extension_dir needs to point to the directory where PHP extensions files are stored. The path can be absolute (i.e. "C:\PHP5\ext") or relative (i.e. ".\ext"). Extensions that are listed lower in the php.ini file need to be located in the extension_dir.

Note: - It is better approach to save php.ini in c:/windows as well.

PHP is now setup on your system. The next step is to choose a web server, and enable it to run PHP. We will go with IIS 7.

Internet Information Services (IIS) 7 is available on Windows Vista, Windows 7, and Windows Server 2008.

You probably heard a lot of bad things about IIS, from various groups. If you were holding off using IIS because of that well now is the time to re-evaluate IIS with version 7. A lot of the problems that plaques IIS were cured way back in IIS 5. Introduced in IIS 7 was a new architecture similar to Apache with its modules. Everything in IIS 7 is a module, an extension that can be removed. Just need a basic HTTP server? No problem! Just remove or deactivate everything but the HTTP service module.

Now let’s install IIS 7 if it is not installed in Windows 7. Go to the Start/Control Panel/Programs/Turn Windows Features on or off and check on the Internet Information Services entry. Activate the World Wide Web Services/Application Development Features/CGI node and also Web Management Tools/IIS Management Console (the latter not shown in the figure).

Installing PHP on Windows 7 - Step 1

Now, start the IIS Management Console; Open up the start menu, press windows+r to bring run dialog, type inetmgr and hit Enter. There, navigate to the Sites/Default Web Site/Handler Mappings node and double-click on the Handler Mappings entry.

Installing PHP on Windows 7 - Step 2


As a result of this, the Actions panel on the right hand side changes. You will now see an option called Add Module Mapping. Clicking on it brings a dialog which you fill out as you can see in the following figure (you may need to adapt the path used to your local system).

Installing PHP on Windows 7 - Step 3


If you do not see the FastCgiModule entry, you probably forgot to check the CGI node when installing IIS. Otherwise, close the Add Module Mapping dialog by pressing OK button. You need to confirm that you want to create a FastCGI application; click Yes.

Installing PHP on Windows 7 - Step 4


If you don't want to save php.ini in c:\windows then restart the IIS else Reboot your pc.

Finally, create a .php page and put it in the root folder (wwwroot) of the IIS site by default C:\Inetpub\wwwroot, e.g. phpinfo.php with a simple phpinfo() call in it like the code given below.
<?php
phpinfo();
?>

Call this page using http://localhost/phpinfo.php, and you are done!

So that's it. Later on I will update this tutorial and will tell you how to configure mysql with php 5.3+ versions running on IIS 7. So stay tuned.

I love your feedback.

Read more...

How to check if checkbox is checked or not using jquery

In this programming tutorial you will learn how to check if checkbox is checked or not using jquery. Jquery is a best javascript library to deal with dom's elements. There are various methods in jquery for this. Let's have a look over it.
How to check if checkbox is checked or not using jquery

//Method 1
if($('#checkBox').attr('checked'))
{
//Your code will come here
} 
else
{
//Your code will come here
}

//Method 2 
if($('#checkBox').is(':checked'))
{
//Your code will come here
}
else
{
//Your code will come here
} 

The above two methods return true or false. Return true if checkbox is checked or false if it is not checked. The third method iterates though all checkboxes with checked attribute defined and performs some action.

So that's it.
I love your feedback.
Read more...

How to remove the last comma from string and replace it with a period in Javascript?

In this programming tutorial you will learn how to remove the last comma from string and replace it with a period in Javascript. I will use regular expression for it. Let's have a look over the following code snippet
How to remove the last comma from string and replace it with a period in Javascript?

str2 will contain the string a,s,d,f,g. I hope you will find this tutorial very handy.

So that's it. I love your feedback.
Read more...

How to remove "," from a string in javascript

In this programming tutorial you will learn how to remove "," from a string in javascript. Couple of days i got requirement to remove "," from the string which is
a,b,c,d,e,f,g,h and make it abcdefgh. I have done it by using javascript. Let's have a look over how to do so
<script type="text/javascript">
        var str1 = "a,b,c,d,e,f,g,h";
        var str2 = str1.replace(/\,/g,"");
    </script>
str2 will contain the string abcdefgh.I hope you will find this tutorial very handy.
So that's it. I love your feedback.
Read more...

How to check if table exists or not in ms sql server database

In this programming tutorial you will learn how to check if table exists or not in ms sql server database. Doing so is quite easy in ms sql server. sp_help command is use for this purpose. So let's have a look over it.
How to check if table exists or not in ms sql server database
sp_help yourtblname
Yeah that's it. If table exists then this command will give you the structure of your table else it will will give you the following error

The object 'blah blah' does not exist in database 'blah blah' or is invalid for this operation.

I love your feedback.
Read more...

How to check if stored procedure exists or not in ms sql server database

In this programming tutorial you will learn how to check if stored procedure exists or not in ms sql server database. It is quite easy in ms sql server with the help of a sp_helptext command. So let's have a look over it.
How to check if stored procedure exists or not in ms sql server database
sp_helptext yourspname
Yeah that's it. If stored procedure exists then this command will give you the text of stored procedure else it will will give you the following error

The object 'blah blah' does not exist in database 'blah blah' or is invalid for this operation.

I love your feedback.
Read more...