diff --git a/static/js/iframe-common.js b/static/js/iframe-common.js index ec9960f24..2919ab1d1 100644 --- a/static/js/iframe-common.js +++ b/static/js/iframe-common.js @@ -80,11 +80,7 @@ export function generateIFrame(domain, answersExperienceFrame) { // For Safari document.body.scrollTop = 0; }); - - window.onpopstate = function() { - iframe.contentWindow.location.replace(calcFrameSrc()); - }; - + registerPopStateListener(); containerEl.appendChild(iframe); // Notify iframe of a click event on parent window @@ -135,6 +131,26 @@ export function generateIFrame(domain, answersExperienceFrame) { }, '#answers-frame'); } +function registerPopStateListener() { + let previousLocationHref = window.location.href; + + /** + * Reloads the iframe with a recalculated src URL. + * Does not reload the iframe if the URL has only changed its hash param. + */ + function popStateListener() { + /** @param {string} url */ + function getURLWithoutHash(url) { + return url.split('#')[0]; + } + if (getURLWithoutHash(previousLocationHref) !== getURLWithoutHash(window.location.href)) { + iframe.contentWindow.location.replace(calcFrameSrc()); + } + previousLocationHref = window.location.href; + } + window.addEventListener('popstate', popStateListener); +} + /** * Sends data to the answers iframe if possible. Otherwise the message is queued * so that it can be sent when the iframe initializes. diff --git a/test-site/.gitignore b/test-site/.gitignore index 44b520f28..27ba7aac7 100644 --- a/test-site/.gitignore +++ b/test-site/.gitignore @@ -13,6 +13,8 @@ webpack-config.js !config/global_config.json !config/locale_config.json !config/index.json +!config/index.ar.json +!config/index.es.json !pages/index.html.hbs !public/iframe_test.html !public/overlay.html \ No newline at end of file