You can use either <object>
or <iframe>
if you really want to, but it is a bit complicated. Further down I have an example, but first the most sane approach -- making the SVG inline.
So, here are two examples. One (#svg01) without pointer-events: all;
and the other (#svg02) without. As I see it (in Firefox) there is no difference. If you click on the white space on either of them, there will be a click event.
document.getElementById('svg01').addEventListener('click', e => {
console.log('click');
});
document.getElementById('svg02').addEventListener('click', e => {
console.log('click');
});
#svg01 {
}
#svg02 {
pointer-events: all;
}
svg {
cursor: pointer;
}
svg:hover circle {
fill: green;
}
<svg id="svg01" height="100" viewBox="0 0 200 100" xmlns="http://www.w3.org/2000/svg">
<circle cx="40" cy="60" r="40" fill="orange" />
<circle cx="100" cy="20" r="20" fill="orange" />
<circle cx="150" cy="70" r="30" fill="orange" />
</svg>
<svg id="svg02" height="100" viewBox="0 0 200 100" xmlns="http://www.w3.org/2000/svg">
<circle cx="40" cy="60" r="40" fill="orange" />
<circle cx="100" cy="20" r="20" fill="orange" />
<circle cx="150" cy="70" r="30" fill="orange" />
</svg>
<object>
and <iframe>
example
If you want to complicate things, here is an example on how to listen for a click event inside of a SVG document, and based on that, call a function on the parent document. This code cannot run on ST, so you need to set up a web server and further more the HTML and SVG document must have same origin.
HTML document
The SVG is either embeded in the HTML document using <object>
or <iframe>
. And then we need something on the window that we can call. Here, I create a simple "API".
<!DOCTYPE html>
<html>
<head>
<script>
window.API = (function(){
let doSomething = () => {
return "hello";
};
return {
doSomething
}
})();
</script>
</head>
<body>
<iframe width="400" height="200" src="svg.svg"></iframe>
<object width="400" height="200" data="svg.svg" type="image/svg+xml"></object>
</body>
</html>
SVG document
In the SVG document we first need to find the API in the parent window. After that we can call the functions on the API. In this case we listen for click events on the entire document.
<?xml version="1.0" encoding="utf-8"?>
<svg id="svg01" viewBox="0 0 200 100" xmlns="http://www.w3.org/2000/svg" pointer-events="all">
<script>//<![CDATA[
var findAPITries = 0;
var API;
function findAPI(win){
while ( (win.API == null) && (win.parent != null) && (win.parent != win) ){
findAPITries++;
if (findAPITries > 7){
return null;
}
win = win.parent;
}
return win.API;
}
document.addEventListener('DOMContentLoaded', e => {
API = findAPI(window);
e.target.addEventListener('click', e => {
console.log(API.doSomething());
});
});
//]]>
</script>
<style>
svg:hover circle {
fill: green;
}
</style>
<circle cx="40" cy="60" r="40" fill="orange" />
<circle cx="100" cy="20" r="20" fill="orange" />
<circle cx="150" cy="70" r="30" fill="orange" />
</svg>
This pattern of calling a "API" is similar to the pattern used in SCORM API Discovery (don't ask :-)).