Alessandro Lacava’s Blog

Google
 

July 27, 2006

Calling a function of the opener window

Filed under: Computer, JavaScript — alessandrolacava @ 5:16 pm

It is possible, using JavaScript, to call a function pertaining to the opener window, that is the window that, calling window.open, opened the current window. The code to use is the following:

JavaScript:
  1. if (window.opener)
  2. {
  3. window.close();
  4. window.opener.foo("bar");
  5. }

First it checks if the opener window is still open. In this case, it closes the current window and call the foo function on the opener window.


July 24, 2006

Creating a Class definition in JavaScript

Filed under: Computer, JavaScript — alessandrolacava @ 6:28 pm

There are different ways to define classes in JavaScript. However, this is the most widely used and accepted at the moment:

JavaScript:
  1. //class
  2. function Person(sLastName, sFirstName, iAge)
  3. {
  4. this.lastName = sLastName;
  5. this.firstName = sFirstName;
  6. this.age = iAge;
  7. this.phoneNumbers = new Array();
  8. }
  9.  
  10. //method
  11. Person.prototype.showFullName = function()
  12. {
  13. alert(this.lastName + " " + this.firstName);
  14. };
  15.  
  16. //instances
  17. var oPerson1 = new Person("Lacava", "Alessandro", 30);
  18. var oPerson2 = new Person("Brown", "John", 50);
  19. oPerson1.phoneNumbers.push("1234567");
  20. oPerson2.phoneNumbers.push("7654321");
  21.  
  22. oPerson1.showFullName(); //outputs Lacava Alessandro
  23. alert(oPerson1.phoneNumbers); //outputs 1234567
  24. oPerson2.showFullName(); //outputs Brown John
  25. alert(oPerson2.phoneNumbers); //outputs 7654321


Next Page »