Disable backward and forward button using javascript

I was facing the same problem and searching for a solution. I got no exact solution, so i wrote my own code.

STEP 1: Before writing the code i analyzed what the browser shortcuts for moving through the history. The shortcuts of different browsers to move through the history are :

given in format keyboard shortcuts (its key value)

Mozilla Firefox

Backspace (8) – 1 page back throught the history

Alt + left arrow (37) – 1 page back through the history

Alt + right arrow (39) – 1 page forward through the history

Shift + Backspace (8) – 1 page forward through the history

Continue reading

How to read XML using Javascript?

The Document Object Model (DOM) as implemented in MSXML provides a programmatic representation of XML documents, fragments, nodes, or node-sets. It also provides an application programming interface for working with XML data.

The following JScript fragments outline the basic process of programming with XML DOM.

var xmlDoc=new ActiveXObject("Microsoft.XMLDOM");

Example:

function loadXML(xmlFilePath)
{
  try //Internet Explorer
  {
    xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
  }
  catch(e)
  {
    try //Firefox, Mozilla, Opera, etc.
    {
      xmlDoc=document.implementation.createDocument("","",null);
    }
    catch(e)
    {
      alert(e.message)
    }
  }
 
  try
  {
    xmlDoc.async=false;
    xmlDoc.load(xmlFilePath); // xmlFilePath - xml file path Ex: test.xml
    return(xmlDoc);
  }
  catch(e)
  {
    alert(e.message)
  }
  return(null);
}

setInterval Method in Javascript

The setInterval() method evaluates a function or an expression repeatedly for the specified number of milliseconds and it returns an interval reference.

Syntax:

variable = setInterval(expression, milliseconds)

or

variable = setInterval(function, milliseconds)

variable holds the interval reference returned by seInterval method.

Example:
Below example display the current time information using setInterval method call the date object.

<html>
<head>
<title> setInterval Method Example </title>
<script language="javascript">
  function startSetInt()
  {
    ref=setInterval("init()",100);
  } 
 
  function init()
  {
    dt = new Date();
    document.getElementById("lbl").innerText = "Timer : " + dt;
    document.getElementById("btnStart").disabled = true;
    document.getElementById("btnStop").disabled  = false;
  }
 
  function stopSetInt()
  {
    clearInterval(ref)
    document.getElementById("lbl").innerText = "Timer : ";
    document.getElementById("btnStart").disabled = false;
    document.getElementById("btnStop").disabled = true;
  }
</script>
</head>
 
<body onload="startSetInt()">
<label id="lbl" name="lbl"></label>
</br>
</br>
 
<input type="button" id="btnStart" value="Start" onclick="startSetInt()" />
<input type="button" id="btnStop" value="Stop" onclick="stopSetInt()" />
 
</body>
</html>

Demo : Click here

JSON Data types and Example

The following are the Basic JSON Data types,

  • Number : integer, real, or floating point
  • String : double-quoted Unicode with backslash escaping
  • Boolean : true and false
  • Array : an ordered sequence of values, comma-separated and enclosed in square brackets
  • Object : collection of key:value pairs, comma-separated and enclosed in curly braces
  • null

Example

{
"firstName": "Praveen",
"lastName": "C",
"address": {
"streetAddress": "KK Nagar",
"city": "Chennai",
"state": "TN",
"postalCode": 6310102
},
"phoneNumbers": [
"091 98943 37266"
]
}

What is JSON?

JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is easy for humans to read and write. It is easy for machines to parse and generate. It is based on a subset of the JavaScript Programming Language, Standard ECMA-262 3rd Edition – December 1999. JSON is a text format that is completely language independent but uses conventions that are familiar to programmers of the C-family of languages, including C, C++, C#, Java, JavaScript, Perl, Python, and many others.

Split Function in Javascript

Split Function: Delimiter :-
The space character ” ” we mentioned will be our delimiter and it is used by the split function as a way of breaking up the string. Every time it sees the delimiter we specified, it will create a new element in an array. The first argument of the split function is the delimiter.

Split() Function:-
The split() function splits text and puts the pieces into an array, with each piece taking up one slot in the array. For example, if you were to split “This is some text”,the split() function would create an array with four slots (zero to 3). The first slot in the array would contain “This”, the second slot would contain “is”, the third “some” and the fourth “text”.

Example:-

 

var stringdata= "1$2$3$4";
var splitdata = stringdata.split("$");
for(i = 0; i < splitdata.length; i++)
{
   document.write("Element " + i + " = " + splitdata [i]);
}


Output
=====
Element 0 = 1
Element 1 = 2
Element 2 = 3
Element 3 = 4

Date Function in Javascript

1.create a date object and store it in a variable

var d= new Date();

 

2.Using the date object,Get the hour,minute,second,month,dayofmonth and year of the Current Date

var hour        = d.getHours();
var minute      = d.getMinutes();
var second      = d.getSeconds();
var month = d.getMonth();
var dayofmonth    = d.getDate();
var year        = d.getYear();

Example:-
Display Next Day of Today

function getNextDate()
{
var date= new Date();
var day = date.getDate();
var month = date.getMonth();
var year = date.getYear();
 
var nextdate= new Date(year, month, day +1);
var nxtDate=nextdate.getMonth()+1+"/"+nextdate.getDate()+"/"+NextDate.getYear();
document.getElementById('DisplaynextDate').value=nxtDate;
//Display Next Date(today+1)
}

Disable Right Mouse Click Using JavaScript

Description:
This script will prevent the default right menu from popping up when the right mouse is clicked on the web page. Works well in IE4+ and NS4+.

To use this script, paste the following code in the <body> section of your HTML document.
 

Disable right click on mouse

 

Function to handle right click if the browser is IE

<script language="JavaScript">
var message=”Right Click is Disabled”;
function ExeForIE()
{
  if (event.button==2)
  {
     alert(message);
     return false;
  }
}
</script>

 

Function to handle right click if the browser is FireFox

<script language=JavaScript>
var message=”Right Click is Disabled”;
function ExeForFFox(evt)
{
  if (document.layersdocument.getElementById&&!document.all )
  {
    if (evt.which==2evt.which==3)
    {
      alert(message);
      return false;
    }
  }
}
 
if (document.layers)
{
  document.captureEvents(Event.MOUSEDOWN);
  document.onmousedown = ExeForFFox;
}
else if (document.all&amp;&amp;!document.getElementById)
{
  document.onmousedown = ExeForIE;
}
document.oncontextmenu=new Function(”alert(message);return false)
</script>