From 27dd806c6cd2cf86ed6bcf0e15f16443c5fe87df Mon Sep 17 00:00:00 2001 From: Timo Tijhof Date: Wed, 19 Nov 2014 00:11:04 +0000 Subject: [PATCH] Omit 'window.' when accessing browsing location The location object is a global, just like document. Using it via 'window' needlessly adds complexity and, for example, makes it harder to catch typos in static analysis. Also standardise on location.href in place of the many different variants, like: location = location.href = location.assign() = And each with 'window', 'document' and without host object. Change-Id: I77510294d8b5bd4b8a1b08e06817762a7839d43d --- includes/installer/WebInstallerPage.php | 2 +- resources/src/mediawiki.page/mediawiki.page.ready.js | 2 +- .../mediawiki.special.javaScriptTest.js | 2 +- .../mediawiki.special/mediawiki.special.preferences.js | 6 +++--- resources/src/mediawiki/mediawiki.Uri.js | 10 ++++------ resources/src/mediawiki/mediawiki.util.js | 4 ++-- 6 files changed, 12 insertions(+), 14 deletions(-) diff --git a/includes/installer/WebInstallerPage.php b/includes/installer/WebInstallerPage.php index d507303595..9ecb24b7b7 100644 --- a/includes/installer/WebInstallerPage.php +++ b/includes/installer/WebInstallerPage.php @@ -1463,7 +1463,7 @@ class WebInstallerComplete extends WebInstallerPage { strpos( $_SERVER['HTTP_USER_AGENT'], 'MSIE' ) !== false ) { // JS appears to be the only method that works consistently with IE7+ - $this->addHtml( "\n\n" ); } else { $this->parent->request->response()->header( "Refresh: 0;url=$lsUrl" ); diff --git a/resources/src/mediawiki.page/mediawiki.page.ready.js b/resources/src/mediawiki.page/mediawiki.page.ready.js index 246cc8172a..36eb9d4f22 100644 --- a/resources/src/mediawiki.page/mediawiki.page.ready.js +++ b/resources/src/mediawiki.page/mediawiki.page.ready.js @@ -7,7 +7,7 @@ // it works only comparing to window.self or window.window (http://stackoverflow.com/q/4850978/319266) if ( window.top !== window.self ) { // Un-trap us from framesets - window.top.location = window.location; + window.top.location.href = location.href; } } diff --git a/resources/src/mediawiki.special/mediawiki.special.javaScriptTest.js b/resources/src/mediawiki.special/mediawiki.special.javaScriptTest.js index d3e8f2993c..3dd65fb514 100644 --- a/resources/src/mediawiki.special/mediawiki.special.javaScriptTest.js +++ b/resources/src/mediawiki.special/mediawiki.special.javaScriptTest.js @@ -25,7 +25,7 @@ // Bind onchange event handler and append to form $html.append( $( select ).change( function () { - window.location = QUnit.url( { useskin: $( this ).val() } ); + location.href = QUnit.url( { useskin: $( this ).val() } ); } ) ); diff --git a/resources/src/mediawiki.special/mediawiki.special.preferences.js b/resources/src/mediawiki.special/mediawiki.special.preferences.js index 1f6429b2e6..043d769329 100644 --- a/resources/src/mediawiki.special/mediawiki.special.preferences.js +++ b/resources/src/mediawiki.special/mediawiki.special.preferences.js @@ -57,7 +57,7 @@ jQuery( function ( $ ) { // therefore save and restore scrollTop to prevent jumping. scrollTop = $( window ).scrollTop(); if ( mode !== 'noHash' ) { - window.location.hash = '#mw-prefsection-' + name; + location.hash = '#mw-prefsection-' + name; } $( window ).scrollTop( scrollTop ); @@ -127,7 +127,7 @@ jQuery( function ( $ ) { // If we've reloaded the page or followed an open-in-new-window, // make the selected tab visible. - hash = window.location.hash; + hash = location.hash; if ( hash.match( /^#mw-prefsection-[\w\-]+/ ) ) { switchPrefTab( hash.replace( '#mw-prefsection-', '' ) ); } @@ -142,7 +142,7 @@ jQuery( function ( $ ) { ( document.documentMode === undefined || document.documentMode >= 8 ) ) { $( window ).on( 'hashchange', function () { - var hash = window.location.hash; + var hash = location.hash; if ( hash.match( /^#mw-prefsection-[\w\-]+/ ) ) { switchPrefTab( hash.replace( '#mw-prefsection-', '' ) ); } else if ( hash === '' ) { diff --git a/resources/src/mediawiki/mediawiki.Uri.js b/resources/src/mediawiki/mediawiki.Uri.js index 5566312859..bb5ddfc0d8 100644 --- a/resources/src/mediawiki/mediawiki.Uri.js +++ b/resources/src/mediawiki/mediawiki.Uri.js @@ -147,8 +147,8 @@ * @param {Object|string} [uri] URI string, or an Object with appropriate properties (especially * another URI object to clone). Object must have non-blank `protocol`, `host`, and `path` * properties. If omitted (or set to `undefined`, `null` or empty string), then an object - * will be created for the default `uri` of this constructor (`document.location` for - * mw.Uri, other values for other instances -- see mw.UriRelative for details). + * will be created for the default `uri` of this constructor (`location.href` for mw.Uri, + * other values for other instances -- see mw.UriRelative for details). * @param {Object|boolean} [options] Object with options, or (backwards compatibility) a boolean * for strictMode * @param {boolean} [options.strictMode=false] Trigger strict mode parsing of the url. @@ -395,9 +395,7 @@ return Uri; }; - // If we are running in a browser, inject the current document location (for relative URLs). - if ( document && document.location && document.location.href ) { - mw.Uri = mw.UriRelative( document.location.href ); - } + // Default to the current browsing location (for relative URLs). + mw.Uri = mw.UriRelative( location.href ); }( mediaWiki, jQuery ) ); diff --git a/resources/src/mediawiki/mediawiki.util.js b/resources/src/mediawiki/mediawiki.util.js index a53cbcbf63..cf56f29e44 100644 --- a/resources/src/mediawiki/mediawiki.util.js +++ b/resources/src/mediawiki/mediawiki.util.js @@ -151,12 +151,12 @@ * Returns null if not found. * * @param {string} param The parameter name. - * @param {string} [url=document.location.href] URL to search through, defaulting to the current document's URL. + * @param {string} [url=location.href] URL to search through, defaulting to the current browsing location. * @return {Mixed} Parameter value or null. */ getParamValue: function ( param, url ) { if ( url === undefined ) { - url = document.location.href; + url = location.href; } // Get last match, stop at hash var re = new RegExp( '^[^#]*[&?]' + $.escapeRE( param ) + '=([^&#]*)' ), -- 2.20.1