Thursday, 1 August 2013

Dynamically add button, textbox, input, radio elements in html form using JavaScript

Adding Elements like textbox, button, radio button etc in a html form using JavaScript is very simple. JavaScript’s document object has a method called createElement() which can be used to create html elements dynamically.


We had used this function in our tutorial: Dynamic combobox-listbox-drop-down using javascript to add dynamic options to a combo box-listbox. Let us use this function to create textboxes, radio buttons, buttons etc dynamically and add them in our page.


<HTML>
<HEAD>
<TITLE>Dynamically add Textbox, Radio, Button in html Form using JavaScript</TITLE>
<SCRIPT language="javascript">
function add(type) {

//Create an input type dynamically.
var element = document.createElement("input");

//Assign different attributes to the element.
element.setAttribute("type", type);
element.setAttribute("value", type);
element.setAttribute("name", type);


var foo = document.getElementById("fooBar");

//Append the element in page (in span).
foo.appendChild(element);

}
</SCRIPT>
</HEAD>
<BODY>
<FORM>
<H2>Dynamically add element in form.</H2>
Select the element and hit Add to add it in form.
<BR/>
<SELECT name="element">
<OPTION value="button">Button</OPTION>
<OPTION value="text">Textbox</OPTION>
<OPTION value="radio">Radio</OPTION>
</SELECT>

<INPUT type="button" value="Add" onclick="add(document.forms[0].element.value)"/>

<span id="fooBar">&nbsp;</span>

</FORM>
</BODY>
</HTML>


Dynamically add button, textbox, input, radio elements in html form using JavaScript

Ajax Style File Uploading using Hidden iFrame

File uploading using AJAX is not possible. AJAX doesn’t actually post forms to the server, it sends selected data to the server in the form of a POST or GET request. As javascript is not capable of grabbing the file from the users machine and sending it to the server, it’s just not possible with AJAX. You have to resort to regular old form submit.


If you have read/seen it somewhere, then it is not through AJAX. File uploading occurs through an iframe in this case. You have to use a iframe to upload the files. So, you can use iframe to asynchronous upload (Like AJAX , but its not AJAX).


How file uploading is done in Gmail? Use following JavaScript function, if you want to achieve same functionality.


<html>

<script language="Javascript">
function fileUpload(form, action_url, div_id) {
// Create the iframe...
var iframe = document.createElement("iframe");
iframe.setAttribute("id", "upload_iframe");
iframe.setAttribute("name", "upload_iframe");
iframe.setAttribute("width", "0");
iframe.setAttribute("height", "0");
iframe.setAttribute("border", "0");
iframe.setAttribute("style", "width: 0; height: 0; border: none;");

// Add to document...
form.parentNode.appendChild(iframe);
window.frames['upload_iframe'].name = "upload_iframe";

iframeId = document.getElementById("upload_iframe");

// Add event...
var eventHandler = function () {

if (iframeId.detachEvent) iframeId.detachEvent("onload", eventHandler);
else iframeId.removeEventListener("load", eventHandler, false);

// Message from server...
if (iframeId.contentDocument) {
content = iframeId.contentDocument.body.innerHTML;
} else if (iframeId.contentWindow) {
content = iframeId.contentWindow.document.body.innerHTML;
} else if (iframeId.document) {
content = iframeId.document.body.innerHTML;
}

document.getElementById(div_id).innerHTML = content;

// Del the iframe...
setTimeout('iframeId.parentNode.removeChild(iframeId)', 250);
}

if (iframeId.addEventListener) iframeId.addEventListener("load", eventHandler, true);
if (iframeId.attachEvent) iframeId.attachEvent("onload", eventHandler);

// Set properties of form...
form.setAttribute("target", "upload_iframe");
form.setAttribute("action", action_url);
form.setAttribute("method", "post");
form.setAttribute("enctype", "multipart/form-data");
form.setAttribute("encoding", "multipart/form-data");

// Submit the form...
form.submit();

document.getElementById(div_id).innerHTML = "Uploading...";
}
</script>

<!-- index.php could be any script server-side for receive uploads. -->
<form>
<input type="file" name="datafile" /></br>
<input type="button" value="upload"
onClick="fileUpload(this.form,'index.php','upload'); return false;" >
<div id="upload"></div>
</form>

</html>


Ajax Style File Uploading using Hidden iFrame

Tuesday, 30 July 2013

Timezone Dropdown Select List

HTML of a Dropdown list with all the time zones already added.



<select name="DropDownTimezone" id="DropDownTimezone">
<option value="-12.0">(GMT -12:00) Eniwetok, Kwajalein</option>
<option value="-11.0">(GMT -11:00) Midway Island, Samoa</option>
<option value="-10.0">(GMT -10:00) Hawaii</option>
<option value="-9.0">(GMT -9:00) Alaska</option>
<option value="-8.0">(GMT -8:00) Pacific Time (US &amp; Canada)</option>
<option value="-7.0">(GMT -7:00) Mountain Time (US &amp; Canada)</option>
<option value="-6.0">(GMT -6:00) Central Time (US &amp; Canada), Mexico City</option>
<option value="-5.0">(GMT -5:00) Eastern Time (US &amp; Canada), Bogota, Lima</option>
<option value="-4.0">(GMT -4:00) Atlantic Time (Canada), Caracas, La Paz</option>
<option value="-3.5">(GMT -3:30) Newfoundland</option>
<option value="-3.0">(GMT -3:00) Brazil, Buenos Aires, Georgetown</option>
<option value="-2.0">(GMT -2:00) Mid-Atlantic</option>
<option value="-1.0">(GMT -1:00 hour) Azores, Cape Verde Islands</option>
<option value="0.0">(GMT) Western Europe Time, London, Lisbon, Casablanca</option>
<option value="1.0">(GMT +1:00 hour) Brussels, Copenhagen, Madrid, Paris</option>
<option value="2.0">(GMT +2:00) Kaliningrad, South Africa</option>
<option value="3.0">(GMT +3:00) Baghdad, Riyadh, Moscow, St. Petersburg</option>
<option value="3.5">(GMT +3:30) Tehran</option>
<option value="4.0">(GMT +4:00) Abu Dhabi, Muscat, Baku, Tbilisi</option>
<option value="4.5">(GMT +4:30) Kabul</option>
<option value="5.0">(GMT +5:00) Ekaterinburg, Islamabad, Karachi, Tashkent</option>
<option value="5.5">(GMT +5:30) Bombay, Calcutta, Madras, New Delhi</option>
<option value="5.75">(GMT +5:45) Kathmandu</option>
<option value="6.0">(GMT +6:00) Almaty, Dhaka, Colombo</option>
<option value="7.0">(GMT +7:00) Bangkok, Hanoi, Jakarta</option>
<option value="8.0">(GMT +8:00) Beijing, Perth, Singapore, Hong Kong</option>
<option value="9.0">(GMT +9:00) Tokyo, Seoul, Osaka, Sapporo, Yakutsk</option>
<option value="9.5">(GMT +9:30) Adelaide, Darwin</option>
<option value="10.0">(GMT +10:00) Eastern Australia, Guam, Vladivostok</option>
<option value="11.0">(GMT +11:00) Magadan, Solomon Islands, New Caledonia</option>
<option value="12.0">(GMT +12:00) Auckland, Wellington, Fiji, Kamchatka</option>
</select>


Timezone Dropdown Select List

Thursday, 25 July 2013

Style Link Based On Filetypes

CSS CODE



/* external links */
a[href^="http://"] {
padding-right: 13px;
background: url('external.gif') no-repeat center right;
}

/* emails */
a[href^="mailto:"] {
padding-right: 20px;
background: url('email.png') no-repeat center right;
}

/* pdfs */
a[href$=".pdf"] {
padding-right: 18px;
background: url('acrobat.png') no-repeat center right;
}


Style Link Based On Filetypes

Get Direction Form (Google Map)

Google Map Direction Form


HTML CODE



<form action="http://maps.google.com/maps" method="get" target="_blank">

<label for="saddr">Enter your location</label>

<input type="text" name="saddr" />

<input type="hidden" name="daddr" value="Concorde Building, Alkapuri, Vadodara" />

<input type="submit" value="Get directions" /></form>





Get Direction Form (Google Map)

Friday, 19 July 2013

Crystal Report Performance Improvement Tips

How can I improve performance of crystal report in my web application?


Solution: Here is some important tips for improvement of crystal report performance in a web application:

1. Avoid using Linked OLE object if not extremely necessary. Locating OLE object is potentially time consuming.

2. Avoid using Sub-report if you can do same without sub-report. Incorrect use of sub report may have huge impact on performance.

3. Avoid special functions: Page N of M, Total Page Count. This cause the report more time to display first page.

4. Avoid unnecessary use of graphics. This may cause disk I/O which will impact on performance.

5. Remove un-used objects from report.

6. Remove or suppress unnecessary report section.

7. If summaries are used in the report, use conditional formulas instead of running totals when ever possible.

8. Whenever possible, limit records through Record selection Formula, not suppression. Return only necessary data from your data source.

9.  Perform grouping on database server.

10.Disable report option Verify of First Refresh and Verify Stored Procedure on First Refresh.


Hope this will help.



Crystal Report Performance Improvement Tips

Disable right click on web page and images

Sometimes it is required to prevent your web page images from being copied by another one. You can secure your images by disabling right click or by disabling image context menu on images only. This is an effective approach to prevent your images from being copied or stolen. Here I am going to share the tricks how can you achieve this. This trick will disable right click only on images. Add the below event handler to the img or image tag as shown below:


Disable right click on images


<asp:Image ID="img1" runat="server" ImageUrl=""../ImgLoc/1.png"" oncontextmenu="return false;" />
<img alt="MyImage" src="../ImgLoc/2.png" oncontextmenu="return false;"/>

Disable right click on web page


Similarly we can disable right click on whole page by adding oncontextmenu handler in body tag of webpage.

Now this will disable right click on each and every control of a webpage.


<html>
<head>
...
</head>
<body oncontextmenu="return false;" >
...
</body>
</html>

Show alert on right click


<script type="text/javascript"> 
function disableRightClick()
{
alert("Sorry, right click is not allowed !!");
return false;
}
</script>
<body oncontextmenu=" return disableRightClick();">
...
</body>

disablerightclick



Disable right click on web page and images