Skip to content

Commit

Permalink
add global instead of window to partially support nodejs
Browse files Browse the repository at this point in the history
  • Loading branch information
vrichard committed Apr 19, 2017
1 parent ceb07a9 commit 3db0ee3
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 25 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "url-polyfill",
"version": "1.0.3",
"version": "1.0.4",
"description": "Polyfill URL and URLSearchParams",
"main": "url-polyfill.js",
"scripts": {
Expand Down
38 changes: 15 additions & 23 deletions url-polyfill.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
var g = (typeof global !== 'undefined') ? global :
(typeof window !== 'undefined') ? window :
(typeof self !== 'undefined') ? self : this;

(function() {
(function(global) {
/**
* Polyfill URLSearchParams
*
* Inspired from : https://github.com/WebReflection/url-search-params/blob/master/src/url-search-params.js
*/

var checkIfURLSearchParamsIsSupported = function() {
return ('URLSearchParams' in window);
};

var checkIfIteratorIsSupported = function() {
try {
return !!Symbol.iterator;
Expand Down Expand Up @@ -38,7 +37,6 @@
return iterator;
};


var polyfillURLSearchParams= function() {

var URLSearchParams = function(searchString) {
Expand Down Expand Up @@ -67,7 +65,6 @@

var proto = URLSearchParams.prototype;


proto.append = function(name, value) {
if(name in this._entries) {
this._entries[name].push(value.toString());
Expand Down Expand Up @@ -139,20 +136,18 @@
return searchString;
};

window.URLSearchParams = URLSearchParams;
global.URLSearchParams = URLSearchParams;
};

if(!checkIfURLSearchParamsIsSupported()) {
if(!('URLSearchParams' in global)) {
polyfillURLSearchParams();
}

// console.log(new URLSearchParams('a=b&c=d'));

// HTMLAnchorElement

})();
})(g);

(function() {
(function(global) {
/**
* Polyfill URL
*
Expand All @@ -171,7 +166,7 @@


var polyfillURL = function() {
var _URL = window.URL;
var _URL = global.URL;

var URL = function(url, base) {
if(typeof url !== 'string') throw new TypeError('Failed to construct \'URL\': Invalid URL');
Expand Down Expand Up @@ -286,32 +281,29 @@
return _URL.revokeObjectURL.apply(_URL, arguments);
};

window.URL = URL;
global.URL = URL;

};

if(!checkIfURLIsSupported()) {
polyfillURL();
}

if(!('origin' in window.location)) {
if((global.location !== void 0) && !('origin' in global.location)) {
var getOrigin = function() {
return window.location.protocol + '//' + window.location.hostname + (window.location.port ? (':' + window.location.port) : '');
return global.location.protocol + '//' + global.location.hostname + (global.location.port ? (':' + global.location.port) : '');
};

try {
Object.defineProperty(window.location, 'origin', {
Object.defineProperty(global.location, 'origin', {
get: getOrigin,
enumerable: true
});
} catch(e) {
setInterval(function() {
window.location.origin = getOrigin();
global.location.origin = getOrigin();
}, 100);
}
}


// console.log(new URL('https://www.yahoo.com/?fr=yset_ie_syc_oracle&type=orcl_hpset'));

})();
})(g);
2 changes: 1 addition & 1 deletion url-polyfill.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 3db0ee3

Please sign in to comment.