Skip to content
This repository was archived by the owner on Oct 8, 2021. It is now read-only.

Commit 4813c33

Browse files
author
Gabriel Schulhof
committed
Navigation: Click handler correctly handles absolute URL with hash
(cherry picked from commit bdfe15d) Closes gh-7632 Fixes gh-5759
1 parent 6c9b996 commit 4813c33

File tree

3 files changed

+25
-3
lines changed

3 files changed

+25
-3
lines changed

js/jquery.mobile.navigation.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -364,9 +364,10 @@ define( [
364364
// lists and select dialogs, just write a hash in the link they
365365
// create. This means the actual URL path is based on whatever
366366
// the current value of the base tag is at the time this code
367-
// is called. For now we are just assuming that any url with a
368-
// hash in it is an application page reference.
369-
if ( href.search( "#" ) !== -1 ) {
367+
// is called.
368+
if ( href.search( "#" ) !== -1 &&
369+
!( $.mobile.path.isExternal( href ) && $.mobile.path.isAbsoluteUrl( href ) ) ) {
370+
370371
href = href.replace( /[^#]*#/, "" );
371372
if ( !href ) {
372373
//link was an empty hash meant purely

tests/integration/navigation/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
<div id="qunit"></div>
3131

3232
<div id="harmless-default-page" data-nstest-role="page" class="first-page">
33+
<a id="goToGoogle" href="http://www.google.com/#abc">Go to Google</a>
3334
</div>
3435

3536
<div id="foo" data-nstest-role="page" class="foo-class">

tests/integration/navigation/navigation_core.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,26 @@ $.testHelper.delayStart();
1616
$.testHelper.openPage( "#" + location.pathname + location.search );
1717
};
1818

19+
test( "Absolute link with hash works", function() {
20+
var defaultIsPrevented,
21+
theLink = $( "#goToGoogle" ),
22+
theClickHandler = function( event ) {
23+
defaultIsPrevented = !!event.isDefaultPrevented();
24+
if ( event.target === theLink[ 0 ] ) {
25+
event.preventDefault();
26+
}
27+
};
28+
29+
$.mobile.document.one( "click", theClickHandler );
30+
31+
$( "#goToGoogle" ).click();
32+
33+
$.mobile.document.off( "click", theClickHandler );
34+
35+
deepEqual( defaultIsPrevented, false,
36+
"Default is not prevented when clicking on external link with hash" );
37+
});
38+
1939
module('jquery.mobile.navigation.js', {
2040
setup: function() {
2141
$.mobile.navigate.history.stack = [];

0 commit comments

Comments
 (0)