Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
languagejs
// Example code on customer web page
// When opening link such as:
// https://your-domain.com/maps/?mazemap_share_url=https%3A%2F%2Fuse.mazemap.com%2F%23v%3D1%26config%3Dcustomer-example-config%26center%3D10.407538%2C63.432910%26zoom%3D19%26zlevel%3D6%26utm_medium%3Dcustom_share_url

const DEFAULT_MAZEMAP_IFRAME_URL = new URL('https://use.mazemap.com/?config=YOUR_CONFIG');

function setMazeMapIframeUrl(url) {
    const iframe = document.getElementById("mazemap-app-iframe");
    if (url && url.href) {
        iframe.src = url.href;
    }
}

// Read the url
const url = new URL(location.href)

// Get the mazemap share url
const mazemapUrlString = url.searchParams.get('mazemap_share_url')

if (mazemapUrl) {
    try {
      // Attempt to convert the url string into a proper URL
      // If the url is not valid or malformed, this will fail and
      // end up in the catch block.
      const url = new URL(string);

      // After valid url parsing, update the iframe with the shared mazemap url
      document.getElementById("mazemap-app-iframe").src = url.hrefsetMazeMapIframeUrl(url);
    } catch (e) {
        console.error("Error valiadating url", e);
        // Fall back to setting the default iframe URL if there was an error
        setMazeMapIframeUrl(DEFAULT_MAZEMAP_IFRAME_URL.href);
    }
} else {
    // If no MazeMap share url was found, just set the default url
    setMazeMapIframeUrl(DEFAULT_MAZEMAP_IFRAME_URL.href);
}