Printing in JavaScript

May 14, 2008

Have you ever been asked by customer to create Print button, or “Print Article” feature. I am sure it happened to you, and if not learning something new is always a good thing.
JavaScript does not offer to many way to print, actually there is only one method available to you, it is:

window.print();

JavaScript print function performs the same operation as “Print” function of your browser. You can think that this function is useless, but it is not. It can help you creating more user friendly applications. Below example shows how to print a webpage on button click.

< !DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Print Example 1</title>
</head>
<body>
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Praesent nec 
orci. Cras pellentesque velit a libero. Phasellus est justo, mollis ut, 
euismod et, consequat a, odio. Maecenas elit neque, tincidunt sed, 
dignissim vel, molestie eu, justo. Vivamus neque. Suspendisse mollis magna 
quis tortor. Sed malesuada nisl id velit. Vestibulum vel neque. Nam 
vestibulum. Donec ultrices dui et dui. In neque. Vivamus ut augue. 
Aenean lobortis magna ac massa. Suspendisse lacinia. Morbi vulputate 
pharetra felis. Vestibulum faucibus. Integer mattis elementum metus.
 Nulla adipiscing dapibus erat. Proin dignissim pharetra dolor. 
<br />
<input type="button" value="Print" onclick="window.print();" />  
</body>
</html>


You can check live demo here.
Another handy example of using this function is for web pages that server articles. Many times you want to give users the ability to print the article but without any advertisements or graphics, this can be easily accomplished by creating link like this

<a href="exampleprint2.htm" target="_blank">Print this article</a>

target=”_blank” will open web page in new window. The webpage with article invokes window.print(); in onload event of tag.

< !DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Print Example 2</title>
</head>
<body onload="window.print();">
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Praesent nec 
orci. Cras pellentesque velit a libero. Phasellus est justo, mollis ut, 
euismod et, consequat a, odio. Maecenas elit neque, tincidunt sed, 
dignissim vel, molestie eu, justo. Vivamus neque. Suspendisse mollis magna 
quis tortor. Sed malesuada nisl id velit. Vestibulum vel neque. Nam 
vestibulum. Donec ultrices dui et dui. In neque. Vivamus ut augue. 
Aenean lobortis magna ac massa. Suspendisse lacinia. Morbi vulputate 
pharetra felis. Vestibulum faucibus. Integer mattis elementum metus.
 Nulla adipiscing dapibus erat. Proin dignissim pharetra dolor. 
<br />
</body>
</html>


You can check live demo here.

Modal Popup in Ajax Control Toolkit display onload

May 3, 2008

There is a small bug in AjaxControlToolkit when you will add Modal Popup Control Extender,

it is being shown for one second when page is being load. It is not a big issue, but it can be very annoying.

Below file shows the proeblem and the resolution. Resolution is very simple, you just need to add
style=”display:none” to ModalPanel control.

 

<asp :Panel runat="server" ID="ModalPanel" CssClass="modalPopup">
        <asp :Label runat="server" ID="ModalLabel" Text="Modal Panel Label" />
        <br /><br />
        <asp :Button runat="server" ID="OkButton" Text="Ok" />
    </asp>
    <cc1 :ModalPopupExtender ID="ModalPopupExtender1" runat="server"
        PopupControlID="ModalPanel" DropShadow="true" TargetControlID="ShowButton"
        OkControlID="OkButton" BackgroundCssClass="modalBackground" >
    </cc1>

Get the Flash Player to see this player.

How to get Query String in PHP?

April 30, 2008

Recently I was implementing a very basic paging system in PHP. The system also supported search functionality, where search parameter was passed in query string, URL was looking like this:

contactlist.php?searchText=some_search_text&pageno=2

contactlist.php or

contactlist.php?pageno=3

The requirement was quite easy I need to append page number to current query string. For those who do not know what is a query string, a small explanation. Query string is used to pass variables. Let’s look at this url:

contactlist.php?searchText=some_search_text&pageno=2

contactlist.php is a script name

 Everything that comes after “?” is a query string, so in this example it will be searchText=some_search_text&pageno=2. searchText and pageno are variables and some_search_text and 2 are values. Query string has this form variable1=value&variable2=value, where each variable is separated by ampersand character (”&”). 

In PHP we can access query string using superglobal variableRecently I was implementing a very basic paging system in PHP. The system also supported search functionality, where search parameter was passed in query string, URL was looking like this:

contactlist.php?searchText=some_search_text&pageno=2

contactlist.php or

contactlist.php?pageno=3

The requirement was quite easy I need to append page number to current query string. For those who do not know what is a query string, a small explanation. Query string is used to pass variables. Let’s look at this url:

contactlist.php?searchText=some_search_text&pageno=2

contactlist.php is a script name

SERVER, which is an array that contains many useful information. To access query string, use below code:

Recently I was implementing a very basic paging system in PHP. The system also supported search functionality, where search parameter was passed in query string, URL was looking like this:

contactlist.php?searchText=some_search_text&pageno=2

contactlist.php or

contactlist.php?pageno=3

The requirement was quite easy I need to append page number to current query string. For those who do not know what is a query string, a small explanation. Query string is used to pass variables. Let’s look at this url:

contactlist.php?searchText=some_search_text&pageno=2

contactlist.php is a script name

Everything that comes after “?” is a query string, so in this example it will be searchText=some_search_text&pageno=2. searchText and pageno are variables and some_search_text and 2 are values. Query string has this form variable1=value&variable2=value, where each variable is separated by ampersand character (“&”).

In PHP we can access query string using superglobal variable $_SERVER, which is an array that contains many useful information. To access query string, use below code:

$_SERVER['QUERY_STRING']

We should be very careful when appending new variables to query string, because as I showed above, some time query string can be empty and sometimes it can already contains some variables, because of this before appending new variable, we should check if we have to append ampersand character (“&”) before or not. This can be done by simple if statement presented below. Below code example also uses $_SERVER["PHP_SELF"], which returns script name

< ?php
   $self = $_SERVER["PHP_SELF"];
 
  if($_SERVER["QUERY_STRING"]) {
 
    $finalurl = $self . "?" . $_SERVER["QUERY_STRING"] . 
      "&myvariable=myvalue";   
 
  } else {
 
    $finalurl = $self . "?" . "myvariable=myvalue";  
 
  } 
?>

 

I hope this simple example will help some one.