xhr-check.js 1.89 KB
Newer Older
YazhouChen's avatar
YazhouChen committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
/*
 * This script is used to provide feedback if the demo is not working correctly on the local file system due to the same origin policy.
 */

(function() {
  var msg = null;

  var msgTemplate = '<div style="border: 1px solid #ff6666; background: #ffcccc; padding: 30px;">' +
                      '<h1>SVGInject not working correctly</h1>' +
                      '<h2>Don\'t worry: It\'s most likely because you are running this on your local file system.</h2>' +
                      '<p>SVGInject works very well in any browser when run with a web server.</p>' +
                      '<p>Due to the same origin policy for some browsers (Chrome, Safari), SVGs can not be loaded when run on the file system.</p>' +
                      '<h2>How to get this to work:</h2>' +
                      '<ul style="font-size: larger;">' +
                        '<li>Run this with a Web Server</li>' +
                        '<li>Use a Firefox browser</li>' +
                      '</ul>' +
                      '<p>There are also other possible solutions (--allow-file-access-from-files flag in Chrome) you might want to look for to run this example on the file system.</p>' +
                    '</div>';

  function createElementFromHTML(htmlString) {
    var div = document.createElement('div');
    div.innerHTML = htmlString;
    return div.firstChild; 
  }

  function showMessage() {
    msg = createElementFromHTML(msgTemplate);
    document.body.insertBefore(msg, document.body.firstChild);
  }

  var req = new XMLHttpRequest();
  req.onreadystatechange = function() {
    if (req.readyState == 4) {
      // readyState is DONE
      var status = req.status;
      if (status == 200) {
        // request status is OK
      } else {
        showMessage()
      }
    }
  };
  var script = document.querySelectorAll("script[src$='xhr-check.js']")[0];

  req.open('GET', script.getAttribute('src'), true);
  req.send();
})();