View Javadoc
1   /*
2    * Copyright (c) 2002-2026 Gargoyle Software Inc.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * https://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
14   */
15  package org.htmlunit.javascript.host.event;
16  
17  import org.htmlunit.corejs.javascript.ScriptableObject;
18  import org.htmlunit.javascript.JavaScriptEngine;
19  import org.htmlunit.javascript.configuration.JsxClass;
20  import org.htmlunit.javascript.configuration.JsxConstructor;
21  import org.htmlunit.javascript.configuration.JsxGetter;
22  
23  /**
24   * JavaScript object representing the HashChangeEvent.
25   * @see <a href="https://developer.mozilla.org/en-US/docs/Web/API/window.onhashchange">Mozilla Developer Network</a>
26   * @see <a href="http://msdn.microsoft.com/en-us/library/cc288209.aspx">MSDN</a>
27   *
28   * @author Ronald Brill
29   * @author Marc Guillemot
30   * @author Frank Danek
31   */
32  @JsxClass
33  public class HashChangeEvent extends Event {
34  
35      private String oldURL_ = "";
36      private String newURL_ = "";
37  
38      /**
39       * Creates a new event instance.
40       */
41      public HashChangeEvent() {
42          super("");
43      }
44  
45      /**
46       * Creates a new event instance.
47       *
48       * @param target the event target
49       * @param type the event type
50       * @param oldURL the old URL
51       * @param newURL the new URL
52       */
53      public HashChangeEvent(final EventTarget target, final String type,
54              final String oldURL, final String newURL) {
55          super(target, type);
56          oldURL_ = oldURL;
57          newURL_ = newURL;
58  
59          setBubbles(false);
60          setCancelable(false);
61      }
62  
63      /**
64       * {@inheritDoc}
65       */
66      @Override
67      @JsxConstructor
68      public void jsConstructor(final String type, final ScriptableObject details) {
69          super.jsConstructor(type, details);
70  
71          if (details != null && !JavaScriptEngine.isUndefined(details)) {
72              if (details.has("oldURL", details)) {
73                  oldURL_ = JavaScriptEngine.toString(details.get("oldURL"));
74              }
75  
76              if (details.has("newURL", details)) {
77                  newURL_ = JavaScriptEngine.toString(details.get("newURL"));
78              }
79          }
80      }
81  
82      /**
83       * Returns the old URL.
84       * @return the old URL
85       */
86      @JsxGetter
87      public String getOldURL() {
88          return oldURL_;
89      }
90  
91      /**
92       * Returns the new URL.
93       * @return the new URL
94       */
95      @JsxGetter
96      public String getNewURL() {
97          return newURL_;
98      }
99  }