Tag Archives: Javascript

How to block right click on a page

<script language=”JavaScript”>

function clickIE() {

if (document.all) {alert(“Right Click Not Allowed”);return false;}}function clickNS(e) {

if (document.layers||(document.getElementById&&!document.all)) { if (e.which==2||e.which==3) {alert(“Right Click Not Allowed”);return false;}}}

if (document.layers) {document.captureEvents(Event.MOUSEDOWN);document.onmousedown=clickNS;}

else{document.onmouseup=clickNS;document.oncontextmenu=clickIE;}document.oncontextmenu=new Function(“return false”)

</script>

Load page content based on menu using simple javascript

Here i have used a javascript function that will display blcok based on the menu that you clicked .
Paste the below given code in  page and load that so as to get a better idea of how it works . 

<script type=”text/javascript”>
function NextPage($pageID){
for($i=1;$i<=3;$i++){
if(Number($i)==Number($pageID))
document.getElementById(“page”+$i).style.display=’block’
else
document.getElementById(“page”+$i).style.display=’none’
}
}
</script>
<table>
<tr>
<td>
<a href=”javascript:NextPage(‘1’);”>Page1</a>
<a href=”javascript:NextPage(‘2’);”>Page2</a>
<a href=”javascript:NextPage(‘3’);”>Page3</a>
</td>
<td>
<div id=”page1″ style=”display:block”>
<strong> Page 1</strong> <br>
It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using ‘Content here, content here’, making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for ‘lorem ipsum’ will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).

</div>

<div id=”page2″ style=”display:none”>
<strong> Page 2</strong> <br>
It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using ‘Content here, content here’, making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for ‘lorem ipsum’ will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).

</div>

<div id=”page3″ style=”display:none”>
<strong> Page 3</strong> <br>
It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using ‘Content here, content here’, making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for ‘lorem ipsum’ will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).

</div>
</td>
</tr>
</table>

What is Ajax?

AJAX  stands for  Asynchronous JavaScript and XML .
This is not a new language but a new technique using the existing  standards like HTML,Javascript.
Using Ajax,we can fetch the data from the server without loading the whole page and do the DB operations as well . 

Here is an example

<script type=”text/javascript”>
function displaySize(sVal){
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null){
alert (“Your browser does not support AJAX!”);
return;
}
var url=”ajax-page.php?”;
url=url+”&sVal=”+sVal;
url=url+”&sid=”+Math.random();
//alert(url);
xmlHttp.onreadystatechange=stateChangedSize;
xmlHttp.open(“GET”,url,true);
xmlHttp.send(null);
}

function stateChangedSize(){
if (xmlHttp.readyState==4)
document.getElementById(“dimension”).innerHTML=xmlHttp.responseText;
}

function GetXmlHttpObject(){
var xmlHttp=null;
try {// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e) {// Internet Explorer
try {
xmlHttp=new ActiveXObject(“Msxml2.XMLHTTP”);
}
catch (e){
xmlHttp=new ActiveXObject(“Microsoft.XMLHTTP”);
}
}
return xmlHttp;
}
</script>
<table>
<tr>
<td>Select Position</td>
<td>
<select name=”banner_position” onchange=”javascript:displaySize(this.value);”>
<option value=”>Select the position</option>
<option value=’header’  >Header</option>
<option value=’footer’ >Footer</option>
<option value=’sidebar’  >Sidebar</option>
</select>
</td>
</tr>
<tr>
<td>Upload image</td>
<td>
<input type=”file” name=”image” />(Size should be <span id=”dimension”></span>)
</td>
</tr>
</table>

 

Create page “ajax-page.php” and paste the below given code

<?php
switch($_REQUEST[‘sVal’]){
case “header”:echo “780 x 100”;break;
case “footer”:echo “180 x 300”;break;
case “sidebar”:echo “180 x 200”;break;
default:echo “880 x 200”;break;
}
?>

switch statement – Javascript

<script type=”text/javascript”>

function displaySize(sizes){

switch (sizes) {

case “header”:sizeDim=’728 x 90′;break;

case “footer”:sizeDim=’500 x 150′;break;

case “sidebar”:sizeDim=’7263 x 181′;break;

}

document.getElementById(‘dimension’).innerHTML=sizeDim;

}

</script>
<table>

<tr>

<td>Select Position</td> <td>

<select name=”banner_position” onchange=”javascript:displaySize(this.value);”>

<option value=”>Select the position</option>

<option value=’header’  >Header</option>

<option value=’footer’ >Footer</option>

<option value=’sidebar’  >Sidebar</option>

</select>

</td>

</tr>

<tr>

<td>Upload image</td> <td> <input type=”file” name=”image” />(Size should be <span id=”dimension”></span>) </td>

</tr>

</table>

Dynamically Add/Remove Any number of Textboxes

<?php $max_no_boxes=10; ?>
 <script type="text/javascript">
 function addnames() {
 document.getElementById("totalnames").value	=
 Number(document.getElementById("totalnames").value) +	Number(1);
 if(Number(document.getElementById("totalnames").value)>=
 Number(<?php echo $max_no_boxes ?>)){
 document.getElementById("totalnames").value=<?php echo $max_no_boxes ?>;
 document.getElementById("nameButton").style.display="none";
 }
 document.getElementById("name"+document.getElementById("totalnames").value)
 .style.display="block";
 }
 function removenames() {
 document.getElementById("name"+document.getElementById("totalnames").
 value).style.display="none";
 document.getElementById("totalnames").value	=
 Number(document.getElementById("totalnames").value) -	Number(1);
 if(Number(document.getElementById("totalnames").value)<
 Number(<?php echo $max_no_boxes ?>))
 document.getElementById("nameButton").style.display="block";
 }
 </script>

 <table>
 <tr>
 <td valign="top">Name</td>
 <td valign="top">
 <div class="project_right">
 <?php for($i=2;$i<=$max_no_boxes;$i++) { 	$nameValue	=	"name".$i; ?>
  <div style="display:<?php echo $totalnames>= $i ?"block":"none"; ?>"
  id="name<?= $i ?>">
 : <input type="text" name="name<?= $i ?>"  class="projectText"
 value="<?php echo $$nameValue ?>">
 <a href="javascript:return false;" onClick="javascript:removenames();">Remove</a>
 </div>
 <?php } ?>
 <div style="width:175px;; clear: left; float:left">:
 <input type="text" name="name1" class="projectText"
 value="<?php echo $name1 ; ?>">
 </div>
 <div style="width:20px; clear: none; float:left">
 <a id="nameButton" href="javascript:return false;"
 onClick="javascript:addnames();">Add</a>
 </div>
 <input type="hidden" id="totalnames"  name="totalnames"
 value="<?php echo $totalnames==""?1:$totalnames; ?>" />
 </div>
 </td>
 </tr>
 </table>

Submit HTML form to pop up page

<script language="JavaScript" type="text/JavaScript">
	function MM_reloadPage(init) {
	if (init==true) with (navigator) {
	if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
	document.MM_pgW=innerWidth; document.MM_pgH=innerHeight;
	onresize=MM_reloadPage; }}
	else if (innerWidth!=document.MM_pgW ||
	innerHeight!=document.MM_pgH) location.reload();
	}
	MM_reloadPage(true);
	function popupWindow(pageURL,WindowName,features) { //v2.0
	window.open(pageURL,WindowName,features);
	}
</script>

<form method="post" action="test.php" target="list"
onSubmit="popupWindow('test.php','list','scrollbars=yes,width=500, height=400')">
	<input type="text" name="name">
	<input type="submit" value="Submit" name="submit">
</form>

Social Bookmark Sharing option

<a href="http://www.addthis.com/bookmark.php?v=250" 
onmouseover="return addthis_open(this, '', '[URL]', '[TITLE]')" 
onmouseout="addthis_close()" onclick="return addthis_sendto()">
<img src="http://s7.addthis.com/static/btn/lg-share-en.gif" 
width="125" height="16" alt="Bookmark and Share" style="border:0"></a>
<script type="text/javascript" src="http://s7.addthis.com/js/250/addthis_widget.js?pub=zeegal"></script>