Checking for the Presence of a Pop-Up Blocker


var newwindow = window.open(   "",   "popupcheck",   "width=100,height=100,directories=no,menubar=no,  toolbar=no"); 

Admittedly, all the previous phrases relied on the fact that the browser can open pop-ups. By default, most modern browsers allow pop-ups that are opened from the local server, but block pop-ups that are opened from the Internet. Also, various toolbars with their own pop-up blocking mechanism make the life of developers who rely on pop-ups harder.

The bad news first: There is no reliable way to ensure that pop-ups can always be opened. Some blockers support various levels of blocking, some of them allowing certain kinds of pop-ups, some of them disabling all pop-ups. Figure 9.5 shows a typical browser configuration screen.

Figure 9.5. A pop-up blocker configuration.


The following code tries to detect whether a pop-up blocker is available: It opens a window and then checks whether it is there. If yes, it will be closed; otherwise, a pop-up blocker is most probably present:

Checking for a Pop-Up Blocker (popupcheck.html)

<script language="JavaScript"   type="text/javascript"> var newwindow = window.open(   "",   "popupcheck",   "width=100,height=100,directories=no,menubar=no, toolbar=no"); if (newwindow) {   newwindow.close(); } else {   window.alert("Pop-up blocker detected!"); } </script> 

Figure 9.6 shows the result when a pop-up is blocked (also visible from the notification bar on top of the browser's content area).

Figure 9.6. A pop-up blocker has been detected.


Tip

The default behavior of most pop-up blockers is that those pop-ups are allowed that are triggered by a user action. So do not try to call window.open() when the page has loaded, but rather call window.open() when the user clicks on something. But still you have no guarantee that the pop-up will actually appear.


Using JavaScript Frames

Working with frames from JavaScript is quite similar to working with the window object. Although frames miss some features a window object has, such as the open() method or ways to resize or reposition it, the basic properties of a frame are the same: The document property points to the document object that is loaded in the frame, and there are further properties like location.

The frames property of the main window (where the HTML document with the <frameset> element resides) is an array with all frames defined in that frameset. You can then access the frames either via their index (zero-based: window.frames[0], window.frames[1], …) or via their name attribute: window.frames["nameOfFrame"].

From every individual frame document (that is, documents that are loaded within a frame, even if they contain <frameset> elements as well), the frame structure can be accessed: parent references the document of the parent frame structure, whereas top jumps to the main document.





JavaScript Phrasebook(c) Essential Code and Commands
JavaScript Phrasebook
ISBN: 0672328801
EAN: 2147483647
Year: 2006
Pages: 178

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net