Skip to content

Instantly share code, notes, and snippets.

@leonderijke
Last active August 2, 2025 16:37
Show Gist options
  • Save leonderijke/c5cf7c5b2e424c0061d2 to your computer and use it in GitHub Desktop.
Save leonderijke/c5cf7c5b2e424c0061d2 to your computer and use it in GitHub Desktop.

Revisions

  1. leonderijke revised this gist Dec 15, 2014. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion svgfixer.js
    Original file line number Diff line number Diff line change
    @@ -23,7 +23,6 @@

    /**
    * Current URL, without the hash
    * @type {[type]}
    */
    var baseUrl = window.location.href
    .replace(window.location.hash, "");
  2. leonderijke revised this gist Dec 15, 2014. 1 changed file with 6 additions and 1 deletion.
    7 changes: 6 additions & 1 deletion svgfixer.js
    Original file line number Diff line number Diff line change
    @@ -21,7 +21,12 @@
    */
    document.addEventListener("DOMContentLoaded", function() {

    var baseUrl = window.location;
    /**
    * Current URL, without the hash
    * @type {[type]}
    */
    var baseUrl = window.location.href
    .replace(window.location.hash, "");

    /**
    * Find all `use` elements with a namespaced `href` attribute, e.g.
  3. leonderijke created this gist Dec 15, 2014.
    56 changes: 56 additions & 0 deletions svgfixer.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,56 @@
    /**
    * SVG Fixer
    *
    * Fixes references to inline SVG elements when the <base> tag is in use.
    * Firefox won't display SVG icons referenced with
    * `<svg><use xlink:href="#id-of-icon-def"></use></svg>` when the <base> tag is on the page.
    *
    * More info:
    * - http://stackoverflow.com/a/18265336/796152
    * - http://www.w3.org/TR/SVG/linking.html
    *
    * One would think that setting the `xml:base` attribute fixes things,
    * but that is being removed from the platform: https://code.google.com/p/chromium/issues/detail?id=341854
    */

    (function(document, window) {
    "use strict";

    /**
    * Initialize the SVG Fixer after the DOM is ready
    */
    document.addEventListener("DOMContentLoaded", function() {

    var baseUrl = window.location;

    /**
    * Find all `use` elements with a namespaced `href` attribute, e.g.
    * <use xlink:href="#some-id"></use>
    *
    * See: http://stackoverflow.com/a/23047888/796152
    */
    [].slice.call(document.querySelectorAll("use[*|href]"))

    /**
    * Filter out all elements whose namespaced `href` attribute doesn't
    * start with `#` (i.e. all non-relative IRI's)
    *
    * Note: we're assuming the `xlink` prefix for the XLink namespace!
    */
    .filter(function(element) {
    return (element.getAttribute("xlink:href").indexOf("#") === 0);
    })

    /**
    * Prepend `window.location` to the namespaced `href` attribute value,
    * in order to make it an absolute IRI
    *
    * Note: we're assuming the `xlink` prefix for the XLink namespace!
    */
    .forEach(function(element) {
    element.setAttribute("xlink:href", baseUrl + element.getAttribute("xlink:href"));
    });

    }, false);

    }(document, window));