From 112261b8a764dd8677d4fc7ad96da96114fa3d72 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Radek=20=C5=A0er=C3=BD?= Date: Mon, 27 May 2019 12:15:01 +0200 Subject: [PATCH] #163 IE11 bug when absolute URL is used - do not use new URL constructor, use custom function instead - changed condition whether to use ajax when absolute URL is used --- nette.ajax.js | 40 ++++++++++++++++++++++++++++++++++++---- 1 file changed, 36 insertions(+), 4 deletions(-) diff --git a/nette.ajax.js b/nette.ajax.js index 94cc8f7..518b2b6 100755 --- a/nette.ajax.js +++ b/nette.ajax.js @@ -15,6 +15,36 @@ if (typeof $ !== 'function') { return console.error('nette.ajax.js: jQuery is missing, load it please'); } + // Inspired by https://stackoverflow.com/questions/736513/how-do-i-parse-a-url-into-hostname-and-path-in-javascript + var getLocation = function(href) { + var k = ['protocol', 'hostname', 'host', 'pathname', 'port', 'search', 'hash', 'href']; + var a = document.createElement('a'); + + a.href = href; + + // IE doesn't populate all link properties when setting .href with a relative URL, + // however .href will return an absolute URL which then can be used on itself + // to populate these additional fields. + if (a.host === '') { + a.href = a.href; + } + + for (var r = {}, i = 0; i < k.length; i++) { + r[k[i]] = a[k[i]]; + } + + // IE doesn't return the leading / in pathname + if (r.pathname === '') { + r.pathname = '/'; + } else if (r.pathname[0] !== '/') { + r.pathname = '/' + r.pathname; + } + + r.toString = function() { return a.href; }; + + return r; + }; + var nette = function () { var inner = { self: this, @@ -335,8 +365,10 @@ $.nette.ext('validation', { // Check if URL is absolute if (/(?:^[a-z][a-z0-9+.-]*:|\/\/)/.test(urlToValidate)) { // Parse absolute URL - var parsedUrl = new URL(urlToValidate); - if (/:|^#/.test(parsedUrl['pathname'] + parsedUrl['search'] + parsedUrl['hash'])) return false; + var parsedUrl = getLocation(urlToValidate); + if (location.pathname === parsedUrl.pathname && location.search === parsedUrl.search && parsedUrl.hash) { + return false; + } } else { if (/:|^#/.test(urlToValidate)) return false; } @@ -400,7 +432,7 @@ $.nette.ext('forms', { data[name + '.y'] = dataOffset[1]; } } - + // https://developer.mozilla.org/en-US/docs/Web/Guide/Using_FormData_Objects#Sending_files_using_a_FormData_object var formMethod = analyze.form.attr('method'); if (formMethod && formMethod.toLowerCase() === 'post' && 'FormData' in window) { @@ -414,7 +446,7 @@ $.nette.ext('forms', { formData.append(i, originalData[i]); } } - + // remove empty file inputs as these causes Safari 11 to stall // https://stackoverflow.com/questions/49672992/ajax-request-fails-when-sending-formdata-including-empty-file-input-in-safari if (formData.entries && navigator.userAgent.match(/version\/11(\.[0-9]*)? safari/i)) {