This is a similar question but I would like to know how to access the SVG's JavaScript functions when they are not embedded but are imported from an external file:
Call svg javascript function inside html javascript function
So, I'll attempt to make my question clear and hopefully not too convoluted.
I have main.html and main.js.
I also have someSVG.svg which imports someSVG.js which has a function, foo().
main.html imports someSVG.svg as an object element.
How do I call foo() from main.js?
Provided you embed the SVG with an <object>
tag, you can access the window object of the SVG with
var inner = document.querySelector('object').contentWindow
Just like the window object of the outer window, it provides all global variables that you would find in normal window, for example a script-defined inner.foo()
or inner.document
.
NOTE: Safari supports HTMLObjectElement.contentDocument from version 10, but not .documentWindow.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.