WindowEventHandlers.onhashchange 特性为 WindowEventHandlers 混合 EventHandler 为处理 hashchange 事件。

hashchange event fires when a window's hash changes (see Window.location and HTMLHyperlinkElementUtils.hash ).

句法

Using an event handler:

window.onhashchange = funcRef;
					

Using an HTML event handler:

<body onhashchange="funcRef();">
					

Using an event listener:

To add an event listener, use addEventListener() :

window.addEventListener("hashchange", funcRef, false);
					

参数

funcRef

A reference to a function.

范例

Using an event handler

This example uses an event handler ( window.onhashchange ) to check the new hash value whenever it changes. If it equals #cool-feature , the script logs a message to the console.

function locationHashChanged() {
  if (location.hash === '#cool-feature') {
    console.log("You're visiting a cool feature!");
  }
}
window.onhashchange = locationHashChanged;
					

Using an event listener

This example uses an event listener to log a notification whenever the hash has changed.

function hashHandler() {
  console.log('The hash has changed!');
}
window.addEventListener('hashchange', hashHandler, false);
					

Overriding the hash

This function sets a new hash dynamically, setting it randomly to one of two values.

function changeHash() {
  location.hash = (Math.random() > 0.5) ? 'location1' : 'location2';
}
					

The hashchange event

The dispatched hashchange event has the following properties:

字段 类型 描述
newURL DOMString The new URL to which the window is navigating.
oldURL DOMString The previous URL from which the window was navigated.

Polyfill for event.newURL and event.oldURL

// Let this snippet run before your hashchange event binding code
if (!window.HashChangeEvent)(function(){
  var lastURL = document.URL;
  window.addEventListener("hashchange", function(event){
    Object.defineProperty(event, "oldURL", {enumerable:true,configurable:true,value:lastURL});
    Object.defineProperty(event, "newURL", {enumerable:true,configurable:true,value:document.URL});
    lastURL = document.URL;
  });
}());
					

规范

规范 状态 注释
HTML 实时标准
The definition of 'onhashchange' in that specification.
实时标准
HTML 5.1
在该规范中的 GlobalEventHandlers 定义。
推荐
HTML5
在该规范中的 GlobalEventHandlers 定义。
推荐

浏览器兼容性

The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
桌面 移动
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
onhashchange Chrome 5 Edge 12 Firefox 3.6 IE 8 Opera 10 Safari 5 WebView Android ≤37 Chrome Android 18 Firefox Android 4 Opera Android 10.1 Safari iOS 5 Samsung Internet Android 1.0

图例

完整支持

完整支持

另请参阅

元数据

  • 最后修改:
  1. WindowEventHandlers
  2. 特性
    1. onafterprint
    2. onbeforeprint
    3. onbeforeunload
    4. onhashchange
    5. onlanguagechange
    6. onmessage
    7. onmessageerror
    8. onpopstate
    9. onrejectionhandled
    10. onstorage
    11. onunhandledrejection
    12. onunload
  3. 实现通过:
    1. HTMLBodyElement
    2. HTMLFrameSetElement
    3. Window
  4. HTML DOM 相关页面
    1. BeforeUnloadEvent
    2. DOMStringMap
    3. ErrorEvent
    4. GlobalEventHandlers
    5. HTMLAnchorElement
    6. HTMLAreaElement
    7. HTMLAudioElement
    8. HTMLBRElement
    9. HTMLBaseElement
    10. HTMLBaseFontElement
    11. HTMLBodyElement
    12. HTMLButtonElement
    13. HTMLCanvasElement
    14. HTMLContentElement
    15. HTMLDListElement
    16. HTMLDataElement
    17. HTMLDataListElement
    18. HTMLDialogElement
    19. HTMLDivElement
    20. HTMLDocument
    21. HTMLElement
    22. HTMLEmbedElement
    23. HTMLFieldSetElement
    24. HTMLFormControlsCollection
    25. HTMLFormElement
    26. HTMLFrameSetElement
    27. HTMLHRElement
    28. HTMLHeadElement
    29. HTMLHeadingElement
    30. HTMLHtmlElement
    31. HTMLIFrameElement
    32. HTMLImageElement
    33. HTMLInputElement
    34. HTMLIsIndexElement
    35. HTMLKeygenElement
    36. HTMLLIElement
    37. HTMLLabelElement
    38. HTMLLegendElement
    39. HTMLLinkElement
    40. HTMLMapElement
    41. HTMLMediaElement
    42. HTMLMetaElement
    43. HTMLMeterElement
    44. HTMLModElement
    45. HTMLOListElement
    46. HTMLObjectElement
    47. HTMLOptGroupElement
    48. HTMLOptionElement
    49. HTMLOptionsCollection
    50. HTMLOutputElement
    51. HTMLParagraphElement
    52. HTMLParamElement
    53. HTMLPictureElement
    54. HTMLPreElement
    55. HTMLProgressElement
    56. HTMLQuoteElement
    57. HTMLScriptElement
    58. HTMLSelectElement
    59. HTMLShadowElement
    60. HTMLSourceElement
    61. HTMLSpanElement
    62. HTMLStyleElement
    63. HTMLTableCaptionElement
    64. HTMLTableCellElement
    65. HTMLTableColElement
    66. HTMLTableDataCellElement
    67. HTMLTableElement
    68. HTMLTableHeaderCellElement
    69. HTMLTableRowElement
    70. HTMLTableSectionElement
    71. HTMLTemplateElement
    72. HTMLTextAreaElement
    73. HTMLTimeElement
    74. HTMLTitleElement
    75. HTMLTrackElement
    76. HTMLUListElement
    77. HTMLUnknownElement
    78. HTMLVideoElement
    79. HashChangeEvent
    80. 历史
    81. ImageData
    82. 定位
    83. MessageChannel
    84. MessageEvent
    85. MessagePort
    86. Navigator
    87. NavigatorGeolocation
    88. NavigatorID
    89. NavigatorLanguage
    90. NavigatorOnLine
    91. NavigatorPlugins
    92. PageTransitionEvent
    93. Plugin
    94. PluginArray
    95. PopStateEvent
    96. PortCollection
    97. PromiseRejectionEvent
    98. RadioNodeList
    99. Transferable
    100. ValidityState
    101. Window
    102. WindowBase64
    103. WindowTimers