0

I have an HTML page with external JavaScript module

<script type="text/javascript" src="js/js.js"></script>

and an external SVG map "img/map.svg".

I need to make clickable objects on the map which will in turn call some global JavaScript function myJSFunction defined in "js/js.js". This function will modify the title and contents of the HTML page, hide the SVG map and display a table with some data instead. How can I do this? A naive attempt to set an onclick event in the svg code like the following:

<g
     id="g170"
     onclick="myJSFunction()">

results in error:

Uncaught ReferenceError: myJSFunction is not defined at SVGGElement.onclick

4
  • If you're seeing that error then the function is simply not globally defined. Is it in a document.ready handler, for example? It would help to see a working example of the issue, with all relevant code Commented Dec 10, 2019 at 9:48
  • @RoryMcCrossan I just defined it as function myJSFunction() { alert("clicked!"); }. And the script loading code is placed after <object data="img/map.svg" .... Commented Dec 10, 2019 at 9:52
  • @RoryMcCrossan When I run myJSFunction() from the console in Chrome, it works! Commented Dec 10, 2019 at 9:56
  • @RobertLongson Thank you, window.parent.myJSFunction(); works! Commented Dec 10, 2019 at 10:08

1 Answer 1

2

You can access parent javascript functions via window.parent i.e. window.parent.myJSFunction()

Sign up to request clarification or add additional context in comments.

1 Comment

I faced a security error with this solution: stackoverflow.com/q/59279385/590388

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.