Archive for the 'JavaScript Tutorial' Category

Some Important JavaScript Functions

pop()
This array function is to remove the last element of an array, similarly push() can be used to insert an element.
Example:
var countries = Array(”India”,”America”,”Japan”,”China”);
countries.pop();
window.reload()
This function is used to reload the current browser window.
random()
This function is used to generate a random number. This function will return a random number (fractional) between 0 and 1. This will [...]

JavaScript Interview Questions and Answers

Here are few JavaScript interview questions and answers:
How do you delete the last element of an array in JavaScript?
array_object_variable.pop();
The pop() method in JavaScript will delete the last element of the array. Whereas the push() method will add a new element at the start of the array.
How do you get the random number between 0 and [...]

Cross Domain Frame Handling

The javascript cross domain frame handling is because of same origin policy. There is an work around to communicate cross domains within the frames. To communicate cross domain frame, we can use fragment as reference.
If your parent window is a different domain, you can communicate as below from the frame.
<script language=”javascript”>
parent.location = yourparent window domain [...]

Including a JavaScript File

We can include a javascript file dynamically like other languages in your javascript code. Here is the source code to include a javascript file dynamically.

<script language=”JavaScript”>

function include_js(file)

{

var include_file = document.createElement(’script’);

include_file.type = ‘text/javascript’;

include_file.src = file;

document.getElementsByTagName(’head’)[0].appendChild(include_file);

}

/* include your file */

include_js(’myscript.js’);

</script>
Here I [...]

Associative array in JavaScript

We can use associative arrays in javascript somewhat similar to PHP. Here is an example:

/* Declaring array in javascript */
var opensource = new Array();

/* assigning values to this javascript associative array */

opensource[‘cms’] = ‘Joomla’;
opensource[‘forum’] = ‘phpBB’;
opensource[‘blog’] = ‘Wordpress’;
opensource[‘os’] = ‘Linux’;
opensource[‘database’] = ‘MySQL’;
opensource[‘webserver’] = ‘Apache’;
We can navigate this array as

for(script in opensource)
{
//to get the key for [...]