diff --git a/src/vanilla/baseLocationService.ts b/src/vanilla/baseLocationService.ts index 9c0b1551..15587e69 100644 --- a/src/vanilla/baseLocationService.ts +++ b/src/vanilla/baseLocationService.ts @@ -7,9 +7,12 @@ import { LocationServices } from "../common/coreservices"; import { Disposable } from "../interface"; import { UIRouter } from "../router"; import { LocationLike, HistoryLike } from "./interface"; -import { parseUrl, getParams, buildUrl } from "./utils"; +import { parseUrl, getParams, buildUrl, getCustomEventCtor } from "./utils"; import { isDefined } from "../common/predicates"; import { extend, deregAll, removeFrom } from "../common/common"; + +const Evt: typeof CustomEvent = getCustomEventCtor(); + /** A base `LocationServices` */ export abstract class BaseLocationServices implements LocationServices, Disposable { constructor(router: UIRouter, public fireAfterUpdate: boolean) { @@ -59,7 +62,7 @@ export abstract class BaseLocationServices implements LocationServices, Disposab this._set(null, null, url, replace); if (this.fireAfterUpdate) { - let evt = extend(new Event("locationchange"), { url }); + let evt = extend(new Evt("locationchange"), { url }); this._listeners.forEach(cb => cb(evt)); } } diff --git a/src/vanilla/utils.ts b/src/vanilla/utils.ts index 772d9377..2c63a65d 100644 --- a/src/vanilla/utils.ts +++ b/src/vanilla/utils.ts @@ -78,3 +78,20 @@ export function locationPluginFactory( }; } +export function getCustomEventCtor(): typeof CustomEvent { + // CustomEvent Polyfill + function _CustomEvent(event, params) { + params = params || { bubbles: false, cancelable: false, detail: undefined }; + let evt = document.createEvent( 'CustomEvent' ); + evt.initCustomEvent( event, params.bubbles, params.cancelable, params.detail ); + return evt; + } + _CustomEvent.prototype = Event.prototype; + + try { + new CustomEvent('foo'); + return CustomEvent; + } catch (_err) { + return _CustomEvent as any; + } +}