Skip to content

Commit c5f7f8e

Browse files
committed
Merge pull request #19 from robertbosse/master
Api docs: hashchange event documentation
2 parents 1af31fb + ccdad8c commit c5f7f8e

File tree

1 file changed

+78
-0
lines changed

1 file changed

+78
-0
lines changed

entries/hashtag.xml

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?xml version="1.0"?>
2+
<entry name="hashtag" type="event" return="jQuery">
3+
<title>.hashchange()</title>
4+
<desc>Enables bookmarkable #hash history.</desc>
5+
<longdesc>
6+
<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's hash changes.</p>
7+
<p>In browsers that support it, the native HTML5 window.onhashchange event is used. In IE6/7 (and IE8 operating in "IE7 compatibility" mode), a hidden Iframe is created to allow the back button and hash-based history to work.</p>
8+
<xi:include href="../includes/core-extension-desc.xml" xmlns:xi="http://www.w3.org/2003/XInclude"/>
9+
</longdesc>
10+
<added>1.0</added>
11+
<signature>
12+
<argument name="callback" type="Function" optional="true">
13+
<desc>A function to invoke after the onhashchange event is fired.</desc>
14+
</argument>
15+
</signature>
16+
<example>
17+
<desc>Click, and watch as the magic happens!</desc>
18+
<code><![CDATA[
19+
$(function(){
20+
// Bind an event to window.onhashchange that, when the hash changes, gets the
21+
// hash and adds the class "selected" to any matching nav link.
22+
$(window).hashchange( function(){
23+
var hash = location.hash;
24+
25+
// Set the page title based on the hash.
26+
document.title = 'The hash is ' + ( hash.replace( /^#/, '' ) || 'blank' ) + '.';
27+
28+
// Iterate over all nav links, setting the "selected" class as-appropriate.
29+
$('#nav a').each(function(){
30+
var that = $(this);
31+
that[ that.attr( 'href' ) === hash ? 'addClass' : 'removeClass' ]( 'selected' );
32+
});
33+
})
34+
// Since the event is only triggered when the hash changes, we need to trigger
35+
// the event now, to handle the hash the page may have loaded with.
36+
$(window).hashchange();
37+
});
38+
]]></code>
39+
<html><![CDATA[
40+
<style>
41+
#nav {
42+
font-size: 200%;
43+
}
44+
45+
#nav a {
46+
color: #777;
47+
border: 2px solid #777;
48+
background-color: #ccc;
49+
padding: 0.2em 0.6em;
50+
text-decoration: none;
51+
float: left;
52+
margin-right: 0.3em;
53+
}
54+
55+
#nav a:hover {
56+
color: #999;
57+
border-color: #999;
58+
background: #eee;
59+
}
60+
61+
#nav a.selected,
62+
#nav a.selected:hover {
63+
color: #0a0;
64+
border-color: #0a0;
65+
background: #afa;
66+
}
67+
</style>
68+
69+
<p id="nav">
70+
<a href="#test1">test 1</a>
71+
<a href="#test2">test 2</a>
72+
<a href="#test3">test 3</a>
73+
<a href="#test4">test 4</a>
74+
</p>
75+
]]></html>
76+
</example>
77+
<category slug="events"/>
78+
</entry>

0 commit comments

Comments
 (0)