Archive for the ‘Javascript’ Category

‘null’ is null or not an object

This error occurs when the object/element you are trying to access does not exists in the html page or you did not specify the ID of the element.

or

You are executing a code while the html page has not yet fully loaded. If you need to execute a certain code during page load, this can be done on window.onload event or put the script before the end of the body tag .

, ,

No Comments


How to get Event object of Firefox

  1. function getEvent() {
  2.  if (window.event) return window.event;
  3.  else return getEvent.caller.arguments[0];
  4. }
  5.  
  6. function showMessage(){
  7. var e = getEvent();
  8.  
  9. // you can use this e object
  10. }

,

No Comments


How to refresh the page using javascript

3 Ways to refresh/reload the page using javascript

  1. <input type=”button” value=”Reload Page” onClick=”window.location.reload()”>
  2. <input type=”button” value=”Reload Page” onClick=”history.go(0)”>
  3. <input type=”button” value=”Reload Page” onClick=”window.location.href=window.location.href”>

,

No Comments


How to replace all occurrences in javascript

To replace all occurrences in a string use the “g” modifier without double quote like this

var strvar = “one two one”;

strvar = strvar.replace(/one/g,”1″);

the output of the strvar would be “1 two 1″

,

No Comments


Access denied in XmlHttpRequest

It’s a security thing to prevent people from using AJAX between websites and servers.

Basically, whenever you write a URL in AJAX, it has to be the same URL as the website you are running the script on. Even the www. has to be the same.

,

No Comments


How to Load multiple function in javascript
  1. function AddLoadEvent(functionName) {
  2. var oldOnload = window.onload;
  3.  
  4. if (typeof window.onload != 'function') {
  5. window.onload = functionName;
  6. } else {
  7. window.onload = function() {
  8. oldOnload();
  9. functionName();
  10. }
  11. }
  12.  
  13. }


,

No Comments


How to Call more than one function on loading the web page

If you’re using the following method to invoke more than one function then you’re stuck, you will be wasting you time on debugging trying to figure out what’s the problem.

  1. function callMe1() {
  2. ….
  3. }
  4.  
  5. function callMe2() {
  6. ….
  7. }
  8.  
  9. window.onload = callMe1;
  10. window.onload = callMe2;

callMe2 function will replace callMe1 function, and only callMe2 function will run.

The workaround of this would be using the Anonymous Function

  1. window.onload = function() {
  2.              callMe1();
  3.              callMe2();
  4. }

Another way to overcome this kind of situation is to use the addLoadEvent function

  1. function addLoadEvent(func) {
  2.      var oldonload = window.onload;
  3.      if (typeof window.onload != 'function') {
  4.                window.onload = func;
  5.      } else {
  6.                window.onload = function() {
  7.                      oldonload();
  8.                      func();
  9.                }
  10.      }
  11. }
  12.  
  13. use this as follows
  14.  
  15. addLoadEvent(callMe1);
  16. addLoadEvent(callMe2);

,

No Comments


Calling function in Javascript

Creating a function in javascript is very simple but calling / invoking the function sometime gets tricky when a function don’t have a parameters, and wondering why the function is it not working….

Example:

  1. function countBodyChildren() {
  2.  var body_element = document.getElementsByTagName("body")[0];
  3.  
  4.  alert(body_element.childNodes.length);
  5. }

and you invoke the function like this

  1. window.onload = countBodyChildren();

when you run your program, nothing happens. So, what’s the problem?
In javascript, calling function without parameter should not have an open and close parenthesis, that’s all folk….:)

It should be

  1. window.onload = countBodyChildren;

,

No Comments


Javascript Functions

Function Literal Notation:

var f = function(){ return 1; }

No Comments



SetPageWidth