Thursday, May 31, 2007

How to prevent right-click with JavaScript

I have searched the web for this solution. I dont' remember where I found it but here is. This code disables right-click through JavaScript. Place it in the "head" section of your web page. The code works with Internet Explorer and Firefox.

<script language="javascript">

var isNS = (navigator.appName == "Netscape") ? 1 : 0;

if(navigator.appName == "Netscape")

document.captureEvents(Event.MOUSEDOWNEvent.MOUSEUP);

function mischandler( )
{ return false; }

function mousehandler(e)

{

var myevent = (isNS) ? e : event;

var eventbutton = (isNS) ? myevent.which : myevent.button;

if((eventbutton==2)(eventbutton==3))
return false;

}

document.oncontextmenu = mischandler;

document.onmousedown = mousehandler;
document.onmouseup = mousehandler;

</script>



Refer your friends to this blog:
http://programmingcentral.blogspot.com/

Prevent browser F5 refresh

Few things are more annoying than needing to prevent your web site's visitors from refreshing a page resulting from a form "post".

This solution works with Internet Explorer. Simply place this code in the "head" section of your page.

<script language=javascript>
document.onkeydown = function(){
if(window.event && window.event.keyCode == 116)
{ // Capture and remap F5
window.event.keyCode = 505;
}
if(window.event && window.event.keyCode == 505)
{ // New action for F5
return false;
// Must return false or the browser will refresh anyway
}
}
</script>
Refer your friends to this blog:
http://programmingcentral.blogspot.com/

Wednesday, May 30, 2007

How to loop through HTML Forms collection and validate control values

I have often wondered how to validate form controls that were dynamically generated from a server-side script. Here's my solution in JavaScript:

for (j=0; j<document.theform.elements.length; j++)
if ( document.theForm.elements[j].type == "text" )
{
//Perform validation when the control's type is text
}

if ( document.theForm.elements[j].type == "checkbox" )
{
//Perform validation when the control's type is checkbox
}

if ( isNaN (document.theForm.elements[j].value) )
{
//Perform validation if the control's value property does not evaluate to a number
}

if ( Number (document.theForm.elements[j].value) < 0 )
{
//Perform validation if the control's value property evaluates to a number less than 0
}
}

Refer your friends to this blog:
http://programmingcentral.blogspot.com/

Tuesday, May 29, 2007

How to become a blog author

Programming Central welcomes contributions from all programmers. If you would like to become an author, leave a comment besides any of our posts.

Refer your friends to this blog:
http://programmingcentral.blogspot.com/

Thursday, May 24, 2007

Printing the contents of a IFrame whose content changes dynamically

One of the problems that vexed me recently was how to print

the contents of an IFrame when the content changes dynamically.


  1. Here is my solution:

    Declare a DIV area in the body of the HTML document with an ID attribute value of
    "IFrameArea".

    Declare a JavaScript function that dynamically assigns an IFrame as the innerHTML
    property of the DIV. Note that the dimensions of the IFrame are small. This
    has the effect of making it invisible.


function PrintIFrame(SRC)

{
var URL;

URL = (SRC).toString();

document.getElementById('IFrameArea').innerHTML = "<iframe src='"

+ URL + "' height='1' width='1' frameborder='0' ID='MyIFrame' name='MyIFrame'>
</iframe>"

}



  1. The document that is loaded in the IFrame should have an “onLoad” event
    handler declared
    1. onLoad="javascript:PrintDocument()"
      delcared in the "Body" tab
  2. The document that is loaded in the IFrame should have a a function to perform
    the printing


function SetupToPrint()

{
parent.frames.PrintCounter.focus();
window.print();
}

Refer your friends to this blog:

http://programmingcentral.blogspot.com/

Tuesday, May 22, 2007

Welcome Everyone

Programming Central is a place for programmers to meet and share their ideas.

Instead of spending hours trying to solve those "difficult" programming problems, stop by and see what your fellow community members think.

We encourage everyone to post questions and answers. Share your hard-earned insights to help make this a thriving community.

All languages and platforms are welcome: .NET, Java, web languages (HTML, JavaScript, VB Script, DHTML, AJAX), and open-source languages.

Welcome to Programming Central!

Refer your friends to this blog:
http://programmingcentral.blogspot.com/