Friday, 18 January 2013

JavaScript Window History

JavaScript Window History


Window History

The window.history object can be written without the window prefix.
To protect the privacy of the users, there are limitations to how JavaScript can access this object.
Some methods:
  • history.back() - same as clicking back in the browser
  • history.forward() - same as clicking forward in the browser

Window History Back

The history.back() method loads the previous URL in the history list.
This is the same as clicking the Back button in the browser.

Example

Create a back button on a page:









The output of the code above will be:


Window History Forward

The history forward() method loads the next URL in the history list.
This is the same as clicking the Forward button in the browser.

 


Create a forward button on a page:









The output of the code above will be:


JavaScript Form Validation

JavaScript Form Validation
JavaScript can be used to validate data in HTML forms before sending off the content to a server.
Form data that typically are checked by a JavaScript could be:
  • has the user left required fields empty?
  • has the user entered a valid e-mail address?
  • has the user entered a valid date?
  • has the user entered text in a numeric field?
Required Fields

·         The function below checks if a field has been left empty. If the field is blank, an alert box alerts a message, the function returns false, and the form will not be submitted:
·         function validateForm()
{
var x=document.forms["myForm"]["fname"].value;
if (x==null || x=="")
  {
  alert("First name must be filled out");
  return false;
  }
}
·         The function above could be called when a form is submitted:
Example
·        

First name:



·         E-mail Validation
·         The function below checks if the content has the general syntax of an email.
·         This means that the input data must contain an @ sign and at least one dot (.). Also, the @ must not be the first character of the email address, and the last dot must be present after the @ sign, and minimum 2 characters before the end:
·         function validateForm()
{
var x=document.forms["myForm"]["email"].value;
var atpos=x.indexOf("@");
var dotpos=x.lastIndexOf(".");
if (atpos<1 atpos="atpos" dotpos="dotpos">=x.length)
  {
  alert("Not a valid e-mail address");
  return false;
  }
}
·         The function above could be called when a form is submitted:
Example
·        

Email:


Disable Back Button Of Browser

 Disable Back Button Of Browser Using Java Script

How to prevent user from navigate to previous page using back button of the browser or the back option in the context menu.
One cannot disable the browser back button functionality only thing that can be done is prevent them.
Below is the JavaScript snippets that needs to be placed in the head section of the page where you don’t want the user to revisit using the back button

<script type = "text/javascript" >
 function preventBack(){window.history.forward();}
    setTimeout("preventBack()", 0);
    window.onunload=function(){null};
</script>

Suppose there are two pages Page1.aspx and Page2.aspx and Page1.aspx redirects to Page2.aspx