JavaScript

Javascript: Creating a basic popup window

J

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 check if a value is a number or not a number using JavaScript

H

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

H

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 set a timer in JavasScript

H

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 force a user to only enter numbers into a text field using JavaScript

H

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 decode a JSON string using JavaScript

H

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 suppress JavaScript errors

H

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>

About Author

Ian Carnaghan

I am a software developer and online educator who likes to keep up with all the latest in technology. I also manage cloud infrastructure, continuous monitoring, DevOps processes, security, and continuous integration and deployment.

Follow Me