{"id":28,"date":"2023-08-25T17:32:31","date_gmt":"2023-08-25T17:32:31","guid":{"rendered":"\/\/api.jquerymobile.com\/1.3\/?p=28"},"modified":"2023-08-25T17:32:31","modified_gmt":"2023-08-25T17:32:31","slug":"hashchange","status":"publish","type":"post","link":"https:\/\/api.jquerymobile.com\/1.3\/hashchange\/","title":{"rendered":"hashchange"},"content":{"rendered":"<article id=\"hashchange1\" class=\"entry event\"><h2 class=\"section-title\">\n<span>hashchange event<\/span><span class=\"version-details\">version added: 1.0<\/span>\n<\/h2>\n<div class=\"entry-wrapper\">\n<p class=\"desc\"><strong>Description: <\/strong>Enables bookmarkable #hash history.<\/p>\n<ul class=\"signatures\"><li class=\"signature\"><h4 class=\"name\">jQuery( &quot;.selector&quot; ).on( &quot;hashchange&quot;, function( event ) { ... } )<\/h4><\/li><\/ul>\n<div class=\"longdesc\" id=\"entry-longdesc\">\n\t\t<p>The jQuery Mobile <code>.hashchange()<\/code> event handler enables very basic bookmarkable #hash history by providing a callback function bound to the window.onhashchange event. The onhashchange event fires when a window&apos;s hash changes.<\/p>\n\t\t<p>In browsers that support it, the native HTML5 window.onhashchange event is used. In IE6\/7 (and IE8 operating in &quot;IE7 compatibility&quot; mode), a hidden iframe is created to allow the back button and hash-based history to work.<\/p>\n\t\t<p>This plugin extends jQuery&apos;s built-in  method. If jQuery UI is not loaded, calling the <code>.hashchange()<\/code> method may not fail directly, as the method still exists. However, the expected behavior will not occur.<\/p>\n\t\t<div class=\"syntaxhighlighter javascript\">\n\t<table>\n\t\t<tbody>\n\t\t\t<tr>\n\t\t\t\t\n\t\t\t\t<td class=\"gutter\">\n\t\t\t\t\t\n\t\t\t\t\t\t<div class=\"line n1\">1<\/div>\n\t\t\t\t\t\n\t\t\t\t\t\t<div class=\"line n2\">2<\/div>\n\t\t\t\t\t\n\t\t\t\t\t\t<div class=\"line n3\">3<\/div>\n\t\t\t\t\t\n\t\t\t\t\t\t<div class=\"line n4\">4<\/div>\n\t\t\t\t\t\n\t\t\t\t\t\t<div class=\"line n5\">5<\/div>\n\t\t\t\t\t\n\t\t\t\t\t\t<div class=\"line n6\">6<\/div>\n\t\t\t\t\t\n\t\t\t\t\t\t<div class=\"line n7\">7<\/div>\n\t\t\t\t\t\n\t\t\t\t\t\t<div class=\"line n8\">8<\/div>\n\t\t\t\t\t\n\t\t\t\t\t\t<div class=\"line n9\">9<\/div>\n\t\t\t\t\t\n\t\t\t\t\t\t<div class=\"line n10\">10<\/div>\n\t\t\t\t\t\n\t\t\t\t\t\t<div class=\"line n11\">11<\/div>\n\t\t\t\t\t\n\t\t\t\t\t\t<div class=\"line n12\">12<\/div>\n\t\t\t\t\t\n\t\t\t\t\t\t<div class=\"line n13\">13<\/div>\n\t\t\t\t\t\n\t\t\t\t\t\t<div class=\"line n14\">14<\/div>\n\t\t\t\t\t\n\t\t\t\t\t\t<div class=\"line n15\">15<\/div>\n\t\t\t\t\t\n\t\t\t\t\t\t<div class=\"line n16\">16<\/div>\n\t\t\t\t\t\n\t\t\t\t\t\t<div class=\"line n17\">17<\/div>\n\t\t\t\t\t\n\t\t\t\t\t\t<div class=\"line n18\">18<\/div>\n\t\t\t\t\t\n\t\t\t\t\t\t<div class=\"line n19\">19<\/div>\n\t\t\t\t\t\n\t\t\t\t<\/td>\n\t\t\t\t\n\t\t\t\t<td class=\"code\">\n\t\t\t\t\t<pre><div class=\"container\"><div class=\"line\"><code>$(<span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span>(<span class=\"hljs-params\"><\/span>) <\/span>{<\/code><\/div><\/div><div class=\"container\"><div class=\"line\"><code>  <span class=\"hljs-comment\">\/\/ Bind an event to window.onhashchange that, when the hash changes, gets the<\/span><\/code><\/div><\/div><div class=\"container\"><div class=\"line\"><code>  <span class=\"hljs-comment\">\/\/ hash and adds the class &quot;selected&quot; to any matching nav link.<\/span><\/code><\/div><\/div><div class=\"container\"><div class=\"line\"><code>  $( <span class=\"hljs-built_in\">window<\/span> ).hashchange(<span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span>(<span class=\"hljs-params\"><\/span>) <\/span>{<\/code><\/div><\/div><div class=\"container\"><div class=\"line\"><code>    <span class=\"hljs-keyword\">var<\/span> hash = location.hash;<\/code><\/div><\/div><div class=\"container\"><div class=\"line\"><code> <\/code><\/div><\/div><div class=\"container\"><div class=\"line\"><code>    <span class=\"hljs-comment\">\/\/ Set the page title based on the hash.<\/span><\/code><\/div><\/div><div class=\"container\"><div class=\"line\"><code>    <span class=\"hljs-built_in\">document<\/span>.title = <span class=\"hljs-string\">&quot;The hash is &quot;<\/span> + ( hash.replace( <span class=\"hljs-regexp\">\/^#\/<\/span>, <span class=\"hljs-string\">&quot;&quot;<\/span> ) || <span class=\"hljs-string\">&quot;blank&quot;<\/span> ) + <span class=\"hljs-string\">&quot;.&quot;<\/span>;<\/code><\/div><\/div><div class=\"container\"><div class=\"line\"><code> <\/code><\/div><\/div><div class=\"container\"><div class=\"line\"><code>    <span class=\"hljs-comment\">\/\/ Iterate over all nav links, setting the &quot;selected&quot; class as-appropriate.<\/span><\/code><\/div><\/div><div class=\"container\"><div class=\"line\"><code>    $( <span class=\"hljs-string\">&quot;#nav a&quot;<\/span> ).each(<span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span>(<span class=\"hljs-params\"><\/span>) <\/span>{<\/code><\/div><\/div><div class=\"container\"><div class=\"line\"><code>      <span class=\"hljs-keyword\">var<\/span> that = $( <span class=\"hljs-built_in\">this<\/span> );<\/code><\/div><\/div><div class=\"container\"><div class=\"line\"><code>      that[ that.attr( <span class=\"hljs-string\">&quot;href&quot;<\/span> ) === hash ? <span class=\"hljs-string\">&quot;addClass&quot;<\/span> : <span class=\"hljs-string\">&quot;removeClass&quot;<\/span> ]( <span class=\"hljs-string\">&quot;selected&quot;<\/span> );<\/code><\/div><\/div><div class=\"container\"><div class=\"line\"><code>    });<\/code><\/div><\/div><div class=\"container\"><div class=\"line\"><code>  });<\/code><\/div><\/div><div class=\"container\"><div class=\"line\"><code>  <span class=\"hljs-comment\">\/\/ Since the event is only triggered when the hash changes, we need to trigger<\/span><\/code><\/div><\/div><div class=\"container\"><div class=\"line\"><code>  <span class=\"hljs-comment\">\/\/ the event now, to handle the hash the page may have loaded with.<\/span><\/code><\/div><\/div><div class=\"container\"><div class=\"line\"><code>  $( <span class=\"hljs-built_in\">window<\/span> ).hashchange();<\/code><\/div><\/div><div class=\"container\"><div class=\"line\"><code>});<\/code><\/div><\/div><\/pre>\n\t\t\t\t<\/td>\n\t\t\t<\/tr>\n\t\t<\/tbody>\n\t<\/table>\n<\/div>\n\n\t\t<p>iFrame source: <span id=\"hashtagIframeLink\">example1.html<\/span>\n\t\t\t<iframe id=\"hashtagIframe\" src=\"\/1.3\/resources\/hashchange\/example1.html\" style=\"width:100%;height:90px;border:0px\"><\/iframe>\n\t\t<\/p>\n\t<\/div>\n<\/div><\/article>","protected":false},"excerpt":{"rendered":"<p>Enables bookmarkable #hash history.<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-28","post","type-post","status-publish","format-standard","hentry","category-events"],"_links":{"self":[{"href":"https:\/\/api.jquerymobile.com\/1.3\/wp-json\/wp\/v2\/posts\/28","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/api.jquerymobile.com\/1.3\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/api.jquerymobile.com\/1.3\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/api.jquerymobile.com\/1.3\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/api.jquerymobile.com\/1.3\/wp-json\/wp\/v2\/comments?post=28"}],"version-history":[{"count":1,"href":"https:\/\/api.jquerymobile.com\/1.3\/wp-json\/wp\/v2\/posts\/28\/revisions"}],"predecessor-version":[{"id":29,"href":"https:\/\/api.jquerymobile.com\/1.3\/wp-json\/wp\/v2\/posts\/28\/revisions\/29"}],"wp:attachment":[{"href":"https:\/\/api.jquerymobile.com\/1.3\/wp-json\/wp\/v2\/media?parent=28"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/api.jquerymobile.com\/1.3\/wp-json\/wp\/v2\/categories?post=28"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/api.jquerymobile.com\/1.3\/wp-json\/wp\/v2\/tags?post=28"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}