KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > gargoylesoftware > htmlunit > javascript > host > Window


1 /*
2  * Copyright (c) 2002, 2005 Gargoyle Software Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * 1. Redistributions of source code must retain the above copyright notice,
8  * this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright notice,
10  * this list of conditions and the following disclaimer in the documentation
11  * and/or other materials provided with the distribution.
12  * 3. The end-user documentation included with the redistribution, if any, must
13  * include the following acknowledgment:
14  *
15  * "This product includes software developed by Gargoyle Software Inc.
16  * (http://www.GargoyleSoftware.com/)."
17  *
18  * Alternately, this acknowledgment may appear in the software itself, if
19  * and wherever such third-party acknowledgments normally appear.
20  * 4. The name "Gargoyle Software" must not be used to endorse or promote
21  * products derived from this software without prior written permission.
22  * For written permission, please contact info@GargoyleSoftware.com.
23  * 5. Products derived from this software may not be called "HtmlUnit", nor may
24  * "HtmlUnit" appear in their name, without prior written permission of
25  * Gargoyle Software Inc.
26  *
27  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
28  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
29  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GARGOYLE
30  * SOFTWARE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
31  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
33  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
36  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37  */

38 package com.gargoylesoftware.htmlunit.javascript.host;
39
40 import java.io.IOException JavaDoc;
41 import java.net.MalformedURLException JavaDoc;
42 import java.net.URL JavaDoc;
43 import java.util.Arrays JavaDoc;
44 import java.util.HashMap JavaDoc;
45 import java.util.List JavaDoc;
46 import java.util.Map JavaDoc;
47
48 import org.apache.commons.collections.Transformer;
49 import org.jaxen.JaxenException;
50 import org.jaxen.XPath;
51 import org.mozilla.javascript.Context;
52 import org.mozilla.javascript.Function;
53 import org.mozilla.javascript.NativeArray;
54 import org.mozilla.javascript.Scriptable;
55
56 import com.gargoylesoftware.htmlunit.AlertHandler;
57 import com.gargoylesoftware.htmlunit.ConfirmHandler;
58 import com.gargoylesoftware.htmlunit.ElementNotFoundException;
59 import com.gargoylesoftware.htmlunit.Page;
60 import com.gargoylesoftware.htmlunit.PromptHandler;
61 import com.gargoylesoftware.htmlunit.StatusHandler;
62 import com.gargoylesoftware.htmlunit.TopLevelWindow;
63 import com.gargoylesoftware.htmlunit.WebClient;
64 import com.gargoylesoftware.htmlunit.WebRequestSettings;
65 import com.gargoylesoftware.htmlunit.WebWindow;
66 import com.gargoylesoftware.htmlunit.html.BaseFrame;
67 import com.gargoylesoftware.htmlunit.html.DomNode;
68 import com.gargoylesoftware.htmlunit.html.HtmlElement;
69 import com.gargoylesoftware.htmlunit.html.HtmlPage;
70 import com.gargoylesoftware.htmlunit.html.xpath.HtmlUnitXPath;
71 import com.gargoylesoftware.htmlunit.javascript.ElementArray;
72 import com.gargoylesoftware.htmlunit.javascript.SimpleScriptable;
73
74 /**
75  * A JavaScript object for a Window.
76  *
77  * @version $Revision: 100 $
78  * @author <a HREF="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
79  * @author <a HREF="mailto:chen_jun@users.sourceforge.net">Chen Jun</a>
80  * @author David K. Taylor
81  * @author <a HREF="mailto:cse@dynabean.de">Christian Sell</a>
82  * @author Darrell DeBoer
83  * @author Marc Guillemot
84  * @author Dierk Koenig
85  * @author Daniel Gredler
86  * @author David D. Kilzer
87  * @author Chris Erskine
88  * @see <a HREF="http://msdn.microsoft.com/workshop/author/dhtml/reference/objects/obj_window.asp">
89  * MSDN documentation</a>
90  */

91 public class Window extends SimpleScriptable {
92
93     private static final long serialVersionUID = -7730298149962810325L;
94     private Document document_;
95     private Navigator navigator_;
96     private WebWindow webWindow_;
97     private Screen screen_;
98     private History history_;
99     private Location location_;
100     private Function onload_;
101     private final Map JavaDoc timeoutThreads_ = new HashMap JavaDoc();
102     private int nextTimeoutId_;
103     private String JavaDoc status_ = "";
104     private ElementArray frames_; // has to be a member to have equality (==) working
105

106     /**
107      * Create an instance. The rhino engine requires all host objects
108      * to have a default constructor.
109      */

110     public Window() {
111     }
112
113
114     /**
115      * Javascript constructor. This must be declared in every javascript file because
116      * the rhino engine won't walk up the hierarchy looking for constructors.
117      */

118     public void jsConstructor() {
119     }
120
121
122     /**
123      * The javascript function "alert()"
124      * @param message The message
125      */

126     public void jsxFunction_alert( final String JavaDoc message ) {
127         final AlertHandler handler = getWebWindow().getWebClient().getAlertHandler();
128         if( handler == null ) {
129             getLog().warn("window.alert(\""+message+"\") no alert handler installed");
130         }
131         else {
132             handler.handleAlert(document_.getHtmlPage(), message);
133         }
134     }
135
136
137     /**
138      * The javascript function "confirm()"
139      * @param message The message
140      * @return true if ok was pressed, false if cancel was pressed
141      */

142     public boolean jsxFunction_confirm( final String JavaDoc message ) {
143         final ConfirmHandler handler = getWebWindow().getWebClient().getConfirmHandler();
144         if( handler == null ) {
145             getLog().warn("window.confirm(\""+message+"\") no confirm handler installed");
146             return false;
147         }
148         else {
149             return handler.handleConfirm(document_.getHtmlPage(), message);
150         }
151     }
152
153
154     /**
155      * The javascript function "prompt()"
156      * @param message The message
157      * @return true if ok was pressed, false if cancel was pressed
158      */

159     public String JavaDoc jsxFunction_prompt( final String JavaDoc message ) {
160         final PromptHandler handler = getWebWindow().getWebClient().getPromptHandler();
161         if( handler == null ) {
162             getLog().warn("window.prompt(\""+message+"\") no prompt handler installed");
163             return null;
164         }
165         else {
166             return handler.handlePrompt(document_.getHtmlPage(), message);
167         }
168     }
169
170
171     /**
172      * Return the javascript property "document"
173      * @return The document
174      */

175     public Document jsxGet_document() {
176         return document_;
177     }
178
179
180     /**
181      * Open a new window
182      *
183      * @param context The javascript Context
184      * @param scriptable The object that the function was called on.
185      * @param args The arguments passed to the function.
186      * @param function The function object that was invoked.
187      * @return The newly opened window
188      */

189     public static Object JavaDoc jsxFunction_open(
190         final Context context, final Scriptable scriptable, final Object JavaDoc[] args, final Function function ) {
191
192         final String JavaDoc url = getStringArg(0, args, null);
193         final String JavaDoc windowName = getStringArg(1, args, "");
194         final String JavaDoc features = getStringArg(2, args, null);
195         final boolean replaceCurrentEntryInBrowsingHistory = getBooleanArg(3, args, false);
196         final Window thisWindow = (Window)scriptable;
197
198         if( features != null
199                 || replaceCurrentEntryInBrowsingHistory == true ) {
200
201             thisWindow.getLog().debug(
202                 "Window.open: features and replaceCurrentEntryInBrowsingHistory "
203                 + "not implemented: url=["+url
204                 + "] windowName=["+windowName
205                 + "] features=["+features
206                 + "] replaceCurrentEntry=["+replaceCurrentEntryInBrowsingHistory
207                 + "]");
208         }
209
210         final URL JavaDoc newUrl = thisWindow.makeUrlForOpenWindow(url);
211         final WebWindow newWebWindow= thisWindow.webWindow_.getWebClient().openWindow(
212             newUrl, windowName, thisWindow.webWindow_ );
213         return (Window)newWebWindow.getScriptObject();
214     }
215
216
217     private URL JavaDoc makeUrlForOpenWindow(final String JavaDoc urlString) {
218         if (urlString.length() == 0) {
219             // IE handles "" as "about:blank" in window.open
220
if (getWebWindow().getWebClient().getBrowserVersion().isIE()) {
221                 return WebClient.URL_ABOUT_BLANK;
222             }
223             else {
224                 return null;
225             }
226         }
227
228         try {
229             final Page page = webWindow_.getEnclosedPage();
230             if( page != null && page instanceof HtmlPage ) {
231                 return ((HtmlPage)page).getFullyQualifiedUrl(urlString);
232             }
233             else {
234                 return new URL JavaDoc(urlString);
235             }
236         }
237         catch( final MalformedURLException JavaDoc e ) {
238             getLog().error("Unable to create url for openWindow: relativeUrl=["+urlString+"]", e);
239             return null;
240         }
241     }
242
243
244     /**
245      * Set a chunk of javascript to be invoked at some specified time later.
246      * The invocation occurs only if the window is opened after the delay
247      * and does not contain an other page than the one that originated the setTimeout.
248      *
249      * @param script the code to execute
250      * @param timeout the delay in milliseconds to wait before executing the code
251      * @return the id of the created timer
252      */

253     public int jsxFunction_setTimeout(final String JavaDoc script, final int timeout) {
254         final Runnable JavaDoc runnable = new Runnable JavaDoc() {
255             public void run() {
256                 final Window window = Window.this;
257                 final Page page = window.getWebWindow().getEnclosedPage();
258                 boolean contextEntered = false;
259                 try {
260                     Thread.sleep(timeout);
261                     window.getLog().debug("Executing timeout: " + script);
262                     
263                     final WebWindow webWindow = window.getWebWindow();
264                     // test that the window is always opened and the page the same
265
if (!webWindow.getWebClient().getWebWindows().contains(webWindow)
266                             || webWindow.getEnclosedPage() != page) {
267                         
268                         window.getLog().debug("the page that originated the setTimeout doesnt exist anymore. "
269                                 + "Execution cancelled.");
270                         return;
271                     }
272
273                     // Register this thread with the rhino engine
274
Context.enter();
275                     contextEntered = true;
276                     final HtmlPage htmlPage = window.document_.getHtmlPage();
277                     htmlPage.executeJavaScriptIfPossible(
278                         script, "Window.setTimeout()", true, htmlPage.getDocumentElement());
279                 }
280                 catch( final InterruptedException JavaDoc e ) {
281                     window.getLog().debug("JavaScript timeout thread interrupted; clearTimeout() probably called.");
282                 }
283                 catch( final Exception JavaDoc e ) {
284                     window.getLog().error("Caught exception in Window.setTimeout()", e);
285                 }
286                 finally {
287                     if (contextEntered) {
288                         // Deregister this thread with the rhino engine
289
Context.exit();
290                     }
291                 }
292             }
293         };
294         final int id = nextTimeoutId_++;
295         final String JavaDoc threadName = "HtmlUnit setTimeout() Thread " + id;
296         final Thread JavaDoc setTimeoutThread = new Thread JavaDoc(runnable, threadName);
297         timeoutThreads_.put(new Integer JavaDoc(id), setTimeoutThread);
298         setTimeoutThread.setPriority(Thread.currentThread().getPriority() + 1);
299         setTimeoutThread.start();
300         return id;
301     }
302
303
304     /**
305      * Cancels a time-out previously set with the <tt>setTimeout</tt> method.
306      *
307      * @param timeoutId identifier for the timeout to clear (returned by <tt>setTimeout</tt>)
308      */

309     public void jsxFunction_clearTimeout(final int timeoutId) {
310         final Thread JavaDoc setTimeoutThread = (Thread JavaDoc) timeoutThreads_.get(new Integer JavaDoc(timeoutId));
311         if(setTimeoutThread != null) {
312             setTimeoutThread.interrupt();
313         }
314     }
315
316
317     /**
318      * Return the javascript property "navigator"
319      * @return The document
320      */

321     public Navigator jsxGet_navigator() {
322         return navigator_;
323     }
324
325
326     /**
327      * Return the window property. This is a synonym for "self"
328      * @return A reference to this
329      */

330     public Window jsxGet_window() {
331         return this;
332     }
333
334
335     /**
336      * Return the "self" property
337      * @return this
338      */

339     public Window jsxGet_self() {
340         return this;
341     }
342
343
344     /**
345      * Return the location property
346      * @return The location property
347      */

348     public Location jsxGet_location() {
349         return location_;
350     }
351
352
353     /**
354      * Set the location property. This will cause a reload of the window.
355      * @param newLocation The url of the new content.
356      * @throws IOException when location loading fails
357      */

358     public void jsxSet_location( final String JavaDoc newLocation ) throws IOException JavaDoc {
359         try {
360             final HtmlPage page = (HtmlPage)webWindow_.getEnclosedPage();
361             final URL JavaDoc url = page.getFullyQualifiedUrl(newLocation);
362             
363             getLog().debug(
364                 "window.location=" + newLocation + " (" + url.toExternalForm()
365                         + "), for window named '" + webWindow_.getName() + "'");
366
367             webWindow_.getWebClient().getPage(webWindow_, new WebRequestSettings(url));
368         }
369         catch( final MalformedURLException JavaDoc e ) {
370             getLog().error("jsxSet_location(\""+newLocation+"\") Got MalformedURLException", e);
371             throw e;
372         }
373         catch( final IOException JavaDoc e ) {
374             getLog().error("jsxSet_location(\""+newLocation+"\") Got IOException", e);
375             throw e;
376         }
377     }
378
379
380     /**
381      * Return the "screen" property
382      * @return the screen property
383      */

384     public Screen jsxGet_screen() {
385         return screen_;
386     }
387
388
389     /**
390      * Return the "history" property
391      * @return the "history" property
392      */

393     public History jsxGet_history() {
394         return history_;
395     }
396
397
398     /**
399      * Initialize the object.
400      * @param htmlPage The html page containing the javascript.
401      * @exception Exception If an error occurs.
402      */

403     public void initialize( final HtmlPage htmlPage ) throws Exception JavaDoc {
404
405         webWindow_ = htmlPage.getEnclosingWindow();
406         webWindow_.setScriptObject(this);
407         // Windows don't have corresponding DomNodes so set the domNode
408
// variable to be the page. If this isn't set then SimpleScriptable.get()
409
// won't work properly
410
setDomNode(htmlPage);
411
412         document_ = (Document) makeJavaScriptObject("Document");
413         document_.setDomNode(htmlPage);
414
415         navigator_ = (Navigator)makeJavaScriptObject("Navigator");
416         navigator_.setParentScope(this);
417         screen_ = (Screen)makeJavaScriptObject("Screen");
418         history_ = (History)makeJavaScriptObject("History");
419
420         location_ = (Location)makeJavaScriptObject("Location");
421         location_.initialize(this);
422     }
423
424
425     /**
426      * Return the value of the top property
427      * @return The value of "top"
428      */

429     public SimpleScriptable jsxGet_top() {
430         final WebWindow topWebWindow = webWindow_.getTopWindow();
431         return (SimpleScriptable)topWebWindow.getScriptObject();
432     }
433
434
435     /**
436      * Return the value of the parent property
437      * @return the value of window.parent
438      */

439     public SimpleScriptable jsxGet_parent() {
440         final WebWindow parentWebWindow = webWindow_.getParentWindow();
441         return (SimpleScriptable)parentWebWindow.getScriptObject();
442     }
443
444
445     /**
446      * Return the value of the opener property.
447      * @return the value of window.opener, <code>null</code> for a top level window
448      */

449     public Object JavaDoc jsxGet_opener() {
450         if( webWindow_ instanceof TopLevelWindow ) {
451             final WebWindow opener = ((TopLevelWindow)webWindow_).getOpener();
452             if( opener != null ) {
453                 return (Window)opener.getScriptObject();
454             }
455         }
456
457         return null;
458     }
459
460
461     /**
462      * Return the value of the frames property.
463      * @return The live collection of frames
464      */

465     public ElementArray jsxGet_frames() {
466         if (frames_ == null) {
467             final XPath xpath;
468             try {
469                 xpath = new HtmlUnitXPath("//*[(name() = 'frame' or name() = 'iframe')]");
470             }
471             catch (final JaxenException e) {
472                 // should never occur
473
throw Context.reportRuntimeError("Failed initializing frame collections: " + e.getMessage());
474             }
475             final HtmlPage page = (HtmlPage) getWebWindow().getEnclosedPage();
476             frames_ = (ElementArray) makeJavaScriptObject(ElementArray.JS_OBJECT_NAME);
477             Transformer toEnclosedWindow = new Transformer() {
478                 public Object JavaDoc transform(final Object JavaDoc obj) {
479                     return ((BaseFrame) obj).getEnclosedWindow();
480                 }
481             };
482             frames_.init(page, xpath, toEnclosedWindow);
483         }
484
485         return frames_;
486     }
487
488     /**
489      * Return the WebWindow associated with this Window
490      * @return The WebWindow
491      */

492     public WebWindow getWebWindow() {
493         return webWindow_;
494     }
495
496
497     /**
498      * Set the focus to this element.
499      */

500     public void jsxFunction_focus() {
501         webWindow_.getWebClient().setCurrentWindow(webWindow_);
502     }
503
504
505     /**
506      * Remove focus from this element
507      */

508     public void jsxFunction_blur() {
509         getLog().debug( "Window.blur() not implemented" );
510     }
511
512
513     /**
514      * Close this window
515      */

516     public void jsxFunction_close() {
517         final WebWindow window = ((HtmlPage) getDomNodeOrDie()).getEnclosingWindow();
518         getWebWindow().getWebClient().deregisterWebWindow(window);
519     }
520
521
522     /**
523      * Does nothing.
524      * @param x The horizontal position
525      * @param y The vertical position
526      */

527     public void jsxFunction_moveTo(final int x, final int y) {
528         getLog().debug( "Window.moveTo() not implemented" );
529     }
530
531     /**
532      * Does nothing.
533      * @param x The horizontal position
534      * @param y The vertical position
535      */

536     public void jsxFunction_moveBy(final int x, final int y) {
537         getLog().debug( "Window.moveBy() not implemented" );
538     }
539
540     /**
541      * Does nothing.
542      * @param width The width of the Window in pixel after resize.
543      * @param height The height of the Window in pixel after resize.
544      */

545     public void jsxFunction_resizeTo(final int width, final int height) {
546         getLog().debug( "Window.resizeTo() not implemented" );
547     }
548
549     /**
550      * Does nothing.
551      * @param x The horizontal position to scroll to
552      * @param y The vertical position to scroll to
553      */

554     public void jsxFunction_scroll(final int x, final int y) {
555         getLog().debug( "Window.scroll() not implemented" );
556     }
557
558     /**
559      * Does nothing.
560      * @param x The horizontal distance to scroll by
561      * @param y The vertical distance to scroll by
562      */

563     public void jsxFunction_scrollBy(final int x, final int y) {
564         getLog().debug( "Window.scrollBy() not implemented" );
565     }
566
567     /**
568      * Does nothing.
569      * @param lines The number of lines to scroll down
570      */

571     public void jsxFunction_scrollByLines(final int lines) {
572         getLog().debug( "Window.scrollByLines() not implemented" );
573     }
574
575     /**
576      * Does nothing.
577      * @param pages The number of pages to scroll down
578      */

579     public void jsxFunction_scrollByPages(final int pages) {
580         getLog().debug( "Window.scrollByPages() not implemented" );
581     }
582
583     /**
584      * Does nothing.
585      * @param x The horizontal position to scroll to
586      * @param y The vertical position to scroll to
587      */

588     public void jsxFunction_scrollTo(final int x, final int y) {
589         getLog().debug( "Window.scrollTo() not implemented" );
590     }
591
592     /**
593      * Set the value of the onload event handler.
594      * @param newOnload The new handler
595      */

596     public void jsxSet_onload(final Function newOnload) {
597         onload_ = newOnload;
598     }
599
600     /**
601      * Return the onload event handler function.
602      * @return the onload event handler function.
603      */

604     public Function jsxGet_onload() {
605         if (onload_ == null) {
606             // NB: for IE, the onload of window is the one of the body element but not for Mozilla.
607
final HtmlPage page = (HtmlPage) webWindow_.getEnclosedPage();
608             final List JavaDoc listTagNames = Arrays.asList(new String JavaDoc[] {"body", "frameset"});
609             final List JavaDoc listElements = page.getDocumentElement().getHtmlElementsByTagNames(listTagNames);
610             if (!listElements.isEmpty()) {
611                 return ((HtmlElement) listElements.get(0)).getEventHandler("onload");
612             }
613             else {
614                 return null;
615             }
616         }
617         else {
618             return onload_;
619         }
620     }
621
622     /**
623      * Return the value of the name property
624      * @return The window name
625      */

626     public String JavaDoc jsxGet_name() {
627         return webWindow_.getName();
628     }
629
630      /**
631      * Set the value of the newName property
632      * @param newName The new window name
633      */

634     public void jsxSet_name( final String JavaDoc newName ) {
635         webWindow_.setName(newName);
636     }
637
638     /**
639      * Return the value of the onerror property
640      * @return The value
641      */

642     public String JavaDoc jsxGet_onerror() {
643         getLog().debug("Window.onerror not implemented");
644         return "";
645     }
646
647     /**
648      * Set the value of the onerror property
649      * @param newValue The value
650      */

651     public void jsxSet_onerror( final String JavaDoc newValue) {
652         getLog().debug("Window.onerror not implemented");
653     }
654
655     /**
656      * Return the specified property or {@link #NOT_FOUND} if it could not be found.
657      * @param name The name of the property
658      * @param start The scriptable object that was originally queried for this property
659      * @return The property.
660      */

661     public Object JavaDoc get( final String JavaDoc name, final Scriptable start ) {
662         // If the DomNode hasn't been set yet then do it now.
663
if( getDomNodeOrNull() == null && document_ != null ) {
664             setDomNode( document_.getHtmlPage() );
665         }
666
667         Object JavaDoc result = super.get(name, start);
668
669         final Window thisWindow = (Window) start;
670         // If we are in a frameset or have an iframe then this might be a frame name
671
if( result == NOT_FOUND ) {
672             final DomNode domNode = thisWindow.getDomNodeOrNull();
673             result = getFrameByName( domNode.getPage(), name );
674         }
675
676         // Ask the parent scope, as it may be a variable access like "window.myVar"
677
if( result == NOT_FOUND ) {
678             result = getParentScope().get(name, start);
679             // seems to be a bug in Rhino, workaround as long as the bug is not fixed
680
// https://bugzilla.mozilla.org/show_bug.cgi?id=277462
681
if ("Function".equals(name)) {
682                 result = new ScoperFunctionObject((Function) result, start);
683             }
684         }
685
686         // See if it is an attempt to access an element directly by name or id if we are emulating IE.
687
if (result == NOT_FOUND) {
688             // this tests are quite silly and should be removed when custom JS objects have a clean
689
// way to get the WebClient they are running in.
690
final DomNode domNode = thisWindow.getDomNodeOrNull();
691             if (domNode != null
692                     && domNode.getPage().getWebClient().getBrowserVersion().isIE()) {
693                 final NativeArray array = (NativeArray) thisWindow.document_.jsxFunction_getElementsByName( name );
694                 if (array.getLength() == 1) {
695                     result = array.get(0, this);
696                 }
697                 else if (array.getLength() > 1) {
698                     result = array;
699                 }
700                 else {
701                     result = thisWindow.document_.jsxFunction_getElementById(name);
702                 }
703             }
704         }
705         return result;
706     }
707
708     private Object JavaDoc getFrameByName( final HtmlPage page, final String JavaDoc name ) {
709         try {
710             return page.getFrameByName(name).getScriptObject();
711         }
712         catch (final ElementNotFoundException e) {
713             return NOT_FOUND;
714         }
715     }
716
717     /**
718      * Executes the specified script code as long as the laguage is JavaScript or JScript. Does
719      * nothing if the language specified is VBScript.
720      * @param script the script code to execute
721      * @param language the language of the specified code ("JavaScript", "JScript" or "VBScript")
722      * @see <a HREF="http://msdn.microsoft.com/workshop/author/dhtml/reference/methods/execscript.asp">
723      * MSDN documentation</a>
724      * @return this method always returns <code>null</code>, like Internet Explorer
725      */

726     public Object JavaDoc jsxFunction_execScript(final String JavaDoc script, final String JavaDoc language) {
727         if ("javascript".equalsIgnoreCase(language) || "jscript".equalsIgnoreCase(language)) {
728             final HtmlPage htmlPage = document_.getHtmlPage();
729             final HtmlElement doc = htmlPage.getDocumentElement();
730             htmlPage.executeJavaScriptIfPossible(script, "Window.execScript()", true, doc);
731         }
732         else if ("vbscript".equalsIgnoreCase(language)) {
733             getLog().warn("VBScript not supported in Window.execScript().");
734         }
735         else {
736             // Unrecognized language: use the IE error message ("Invalid class string").
737
throw Context.reportRuntimeError("Invalid class string");
738         }
739         return null;
740     }
741
742     /**
743      * Return the text from the status line.
744      * @return the status line text
745      */

746     public String JavaDoc jsxGet_status() {
747         return status_;
748     }
749
750     /**
751      * Set the text from the status line.
752      * @param message the status line text
753      */

754     public void jsxSet_status( final String JavaDoc message ) {
755         status_ = message;
756
757         final StatusHandler statusHandler = webWindow_.getWebClient().getStatusHandler();
758         if( statusHandler != null ) {
759             statusHandler.statusMessageChanged(webWindow_.getEnclosedPage(), message);
760         }
761     }
762
763     /**
764      * Set a chunk of javascript to be invoked each time a specified number of milliseconds has elapsed
765      * Current implementation does nothing.
766      * @param context The javascript Context
767      * @param scriptable The object that the function was called on.
768      * @param args The arguments passed to the function. First arg must be a function or a string containing
769      * the code to execute. 2nd arg is the interval in milliseconds
770      * @param function The function object that was invoked.
771      * @return the id of the created interval
772      * @see <a HREF="http://msdn.microsoft.com/workshop/author/dhtml/reference/methods/setinterval.asp">
773      * MSDN documentation</a>
774      */

775     public static int jsxFunction_setInterval(final Context context, final Scriptable scriptable,
776         final Object JavaDoc[] args, final Function function ) {
777
778         final Window thisWindow = (Window)scriptable;
779
780         thisWindow.getLog().warn("Current implementation of setInterval does nothing.");
781         return 0;
782     }
783
784     /**
785      * Cancels the interval previously started using the setInterval method.
786      * Current implementation does nothing.
787      * @param iIntervalId specifies the interval to cancel as returned by the setInterval method.
788      * @see <a HREF="http://msdn.microsoft.com/workshop/author/dhtml/reference/methods/clearinterval.asp">
789      * MSDN documentation</a>
790      */

791     public void jsxFunction_clearInterval(final int iIntervalId) {
792         getLog().warn("Current implementation of clearInterval does nothing.");
793     }
794
795
796     /**
797      * Return the innerWidth.
798      * @return a dummy value
799      * @see <a HREF="http://www.mozilla.org/docs/dom/domref/dom_window_ref28.html">Mozilla doc</a>
800      */

801     public int jsxGet_innerWidth() {
802         return 1276; // why this value? this is the current value of my Mozilla
803
}
804
805     /**
806      * Return the outerWidth.
807      * @return a dummy value
808      * @see <a HREF="http://www.mozilla.org/docs/dom/domref/dom_window_ref79.html">Mozilla doc</a>
809      */

810     public int jsxGet_outerWidth() {
811         return 1276; // why this value? this is the current value of my Mozilla
812
}
813
814     /**
815      * Return the innerHeight.
816      * @return a dummy value
817      * @see <a HREF="http://www.mozilla.org/docs/dom/domref/dom_window_ref27.html">Mozilla doc</a>
818      */

819     public int jsxGet_innerHeight() {
820         return 778; // why this value? this is the current value of my Mozilla
821
}
822
823     /**
824      * Return the outer height.
825      * @return a dummy value
826      * @see <a HREF="http://www.mozilla.org/docs/dom/domref/dom_window_ref78.html">Mozilla doc</a>
827      */

828     public int jsxGet_outerHeight() {
829         return 936; // why this value? this is the current value of my Mozilla
830
}
831 }
832
Popular Tags