FRIHOSTFORUMSFAQTOSBLOGSDIRECTORY
You are invited to Log in or Register a Frihost Account!

what does this javascript do

 


imagefree
Code:
var w = window;
if (w.parent.document.getElementsByName) {
  var google_ad_frames = w.parent.document.getElementsByName("google_ads_frame");
  for (var i = 0; i < google_ad_frames.length; i++) {
    var f = google_ad_frames[i];
    if (f.contentDocument) {
      if (w.document == f.contentDocument) {
        f.height = f.width = 0;
      }
    } else if (f.contentWindow) {
      if (w == f.contentWindow) {
        f.height = f.width = 0;
      }
    }
  }
}


copied from
Code:
http://pagead2.googlesyndication.com/pagead/google_adsense_script.js
badai
Code:
var w = window;


The Window object is the top level object in the JavaScript hierarchy. The Window object represents a browser window. A Window object is created automatically with every instance of a <body> or <frameset> tag.

Code:
if (w.parent.document.getElementsByName) {


window.parent returns the parent window

The document object represents the entire HTML document and can be used to access all elements in a page. The document object is part of the Window object and is accessed through the window.document property.

So
Code:
w.parent.document
will access the HTML document in browser window.

The getElementsByName() method returns a collection of objects with the specified NAME if the browser support it. So this statement merely test if the browser support getElementsByName in the document within the main browser window.

If it does then do this

Code:
var google_ad_frames = w.parent.document.getElementsByName("google_ads_frame");


This statement assign a variable google_ad_frames to the the object name google_ads_frame in the document within the main browser window, if the object exist.

The next statement,
Code:
for (var i = 0; i < google_ad_frames.length; i++) {

is a loop to do something x time where x equal to how many google_ads_frame object there is in the document. If there is none, then the “something” (in the loop) will not be executed at all.

Also, in the for statement we assigned a variable i, where the initial value of i zero, and will be incremented by 1 everytime the loop finished.

Let say there is at least one google_ads_frame object was found, the next statement within the curl brackets, { and } will be executed.

Code:
var f = google_ad_frames[i];


This statement created a variable f and assigned its value to the (i + 1)-th google_ad_frames object in the document. If it is the first time the loop is executed, the value of i is 0, which mean the first google_ad_frames object in the document. In javascript and most programming language, counter always start with 0 instead of 1. The first number is always 0.

Next statement,
Code:
 if (f. contentDocument) {


check if the google_ad_frames object contain any iframe (inline frame, frame within HTML document).

contentDocument returns the iframe's document as an HTML object.

If it does contain any iframe, the next statement will be excuted:

Code:
if (w.document == f.contentDocument) {


This statement will compare if the HTML page of the google_ad_frames object is exactly the same with iframe within it. If it does the next statement will be excuted:

Code:
f.height = f.width = 0;


This statement simply set the width and height of that particular google_ad_frames object to zero, which effectively make it disappear from the naked eyes.

Following that statement is the close curl bracket, which end what should be done for the if (w.document == f.contentDocument) statement.

The next statement
Code:
} else if (f.contentWindow) {
is to be executed if the
Code:
if (f. contentDocument)
do not find any iframe and at the same time check if there exist any window object within the google_ad_frames object.

If there is no window object within the google_ad_frames object, the rest of the statement will be ignore. If there is a window object, then the next statement,
Code:
if (w == f.contentWindow) {
will be executed.

This statement will check if the google_ad_frames object is exactly the same with the window within it.

If it is, the next stament,
Code:
f.height = f.width = 0;
will be excuted.

The rest of the close curl brackets just to end the statement block for each of if, for and else above.

In a nutshell, it simply check if there is any duplicate of google_ad_frames object in the current browser windows so you won’t see the same ad twice.
Reply to topic    Frihost Forum Index -> Scripting -> Html, CSS and Javascript

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2007 Frihost, forums powered by phpBB.