2 * JavaScript to update page URL when a redirect is viewed, ensuring that the
3 * page is scrolled to the id when it's a redirect with fragment.
5 * This is loaded in the top queue, so avoid unnecessary dependencies
6 * like mediawiki.Title or mediawiki.Uri.
9 var profile
= $.client
.profile(),
10 canonical
= mw
.config
.get( 'wgInternalRedirectTargetUrl' ),
12 node
, shouldChangeFragment
, index
;
14 index
= canonical
.indexOf( '#' );
16 fragment
= canonical
.slice( index
);
19 // Never override the fragment if the user intended to look at a different section
20 shouldChangeFragment
= fragment
&& !location
.hash
;
22 // Replace the whole URL if possible, otherwise just change the fragment
23 if ( canonical
&& history
.replaceState
) {
24 if ( !shouldChangeFragment
) {
25 // If the current page view has a fragment already, don't override it
26 canonical
= canonical
.replace( /#.*$/, '' );
27 canonical
+= location
.hash
;
30 // Note that this will update the hash in a modern browser, retaining back behaviour
31 history
.replaceState( /* data= */ history
.state
, /* title= */ document
.title
, /* url= */ canonical
);
32 if ( shouldChangeFragment
) {
33 // Specification for history.replaceState() doesn't require browser to scroll,
34 // so scroll to be sure (see also T110501). Support for IE9 and IE10.
35 node
= document
.getElementById( fragment
.slice( 1 ) );
37 node
.scrollIntoView();
41 } else if ( shouldChangeFragment
) {
42 if ( profile
.layout
=== 'webkit' && profile
.layoutVersion
< 420 ) {
43 // Released Safari w/ WebKit 418.9.1 messes up horribly
44 // Nightlies of 420+ are ok
48 location
.hash
= fragment
;
51 if ( shouldChangeFragment
&& profile
.layout
=== 'gecko' ) {
52 // Mozilla needs to wait until after load, otherwise the window doesn't
53 // scroll. See <https://bugzilla.mozilla.org/show_bug.cgi?id=516293>.
54 // There's no obvious way to detect this programmatically, so we use
55 // version-testing. If Firefox fixes the bug, they'll jump twice, but
56 // better twice than not at all, so make the fix hit future versions as
59 if ( location
.hash
=== fragment
) {
60 location
.hash
= fragment
;
65 }( mediaWiki
, jQuery
) );