Javascript gives you the ability to open new windows in you’re browser. You’ll also have the ability to run any code in these windows, you could even have actions in the one window that will affect another window. The following statement is used to open up a basic popup window window.open(“popup.html”,”PopUp...
How to redirect to a page using Javascript
window.location.href = 'redirect.html';
How to check if a value is a number or not a number using JavaScript
By using the isNaN() JavaScript function. isNaN = Is Not A Number
Examples:
document.write(isNaN(“Hello World!”)); //Will print true since Hello World! is not a number
document.write(isNaN(123456)); //Will print false, since 123456 is a number.
var phoneNumber = 0724956785234;
if(isNaN(phoneNumber) == true){
alert(“Please enter a valid phone number”);
return false;
}
How to encode a URL in JavaScript
There are two JavaScript function that you can use to accomplish this
encodeURIComponent()
Encodes a URI component
Encodes special characters
The following characters: , / ? : @ & = + $ # are encoded
var url=""+encodeURIComponent(url);
encodeURI()
Encode a URI
Encodes special characters but not (except) , / ? : @ & = + $ #
var url=""+encodeURI(url);
How to refresh an IFrame using Javascript
var iframe = document.getElementById('iframe_id');
iframe.src = iframe.src;
iframe_id is the ID of the IFrame
How to change the src (url) of a IFrame using JavaScript
document.getElementById('iframe_id').src = 'newpage.html
iframe_id = The ID for the IFrame
newpage.html = The page that you want to load into the IFrame
How to set a timer in JavasScript
If we want to call a function every 10 seconds using JavaScript we can accomplish it by implementing the following piece of code window.setInterval('yourfunction()', 1000); function yourfunction(){ alert('test'); } With the code above every 10 seconds a JavaScript function with the name yourfunction will be called that will prompt an alert. window.setInterval = Evaluates an expression each...
How to disable and enable HTML form fields using JavaScript
To enable a form field use the below JavaScript
document.getElementById("ELEMENT_ID").disabled = false;
And to disable the form field use the below JavaScript
document.getElementById("ELEMENT_ID").disabled = true;
NOTE: Replace ELEMENT_ID with the form fields ID
How to get the file extension from a filename using Javascript
var filename = 'myFile.jpg';
var ext = filename.split('.').pop(); //We split the filename on every instance where the . is found and then getting the last element in the array using the pop() function
alert(ext);
How to force a user to only enter numbers into a text field using JavaScript
Add the following JavaScript function to your HTML page function isNumberKey(evt) { var charCode = (evt.which) ? evt.which : event.keyCode if (charCode > 31 && (charCode < 48 || charCode > 57)) return false; return true; } The above function checks the input of the user and then returns false if no number has been entered and true if a number has been...
How to covert a string to lowercase using JavaScript
Use the string.toLowerCase() function as the example below
<script type="text/javascript">
var str="I NEED THIS STRING TO BE CONVERTED TO LOWER CASE";
document.write(str.toLowerCase());
</script>
How to go back to the previous page with JavaScript
Use the history.go(-1) function in conjunction with the anchor html element
Example
<a href="javascript: history.go(-1);">Go back to previous page</a>
How to get the the value for a form element using JavaScript by ID
If you have a text input box with ID age and you want to obtain the value for the text box via JavaScript use the JavaScript function getElementById() to get the value
Html input text box
<input type="text" name="age" id="age" value="12" />
JavaScript function
<script type="text/javascript">
var age = document.getElementById('age').value;
</script>
How to decode a JSON string using JavaScript
If you have a JSON encoded string that you might have received from a ajax scriptand you need to decode it using JavaScript you can use the eval() function to decode the string. The eval() function evaluates or executes an given argument.
Example:
var string = '{"id" : 32}';
var d = eval('(' + string + ')');
alert(d.id); //will alert 32
How to get the current date using JavaScript
This can be obtained by using the Pre-defined object (Date) in JavaScript
Example:
document.write("The current date is:"+Date())
How to get the size of a array using JavaScript
To get the total size of the array elements within an JavaScript array the JavaScriptlength property can be used.
E.g.
var arr = new Array();
arr[0] = 'val 1';
arr[1] = 'val 2';
alert(arr.length); //Will output 2
How to suppress JavaScript errors
Create a JavaScript function called suppressJS()
function suppressJS(){return true;}
Then within the head tags add the following code
window.onerror=suppressJS;
Your complete code will look like this
<script type="text/javascript">
<!--
function suppressJS(){return true;}
window.onerror=suppressJS;
//-->
</script>
How to get the full url using JavaScript?
Use the following code to obtain the full URL
[code]var fullURL = window.location.href;
alert(fullURL);[/code]
location.href holds the current URL for reference purposes and can be changed at any time. Once changed the page will reload with the new assigned URL