accessing new commands from an outer page to use within an iframe

14 views
Skip to first unread message

Alex Jordan

unread,
Aug 26, 2025, 7:29:45 PMAug 26
to MathJax Users
I have a web page "A" that I have complete control over, including that it loads MathJax 3 that I can configure. This page is loaded in an iframe within page "B", and I do not have control over page "B". Page "B" also loads MathJax 3. And page "B" has at least one piece of math content that defines a macro. Let's say:
```
<span>\(\newcommand{\definiteintegral}[4]{\int_{#1}^{#2}\,#3\,d#4}\)</span>
```
The iframe doesn't load until everything else in page "B" has loaded.

Is there anything I can do with script in page "A" to access all such new commands from the MathJax instance in page "B", and load them into the instance of MathJax that is running in the iframe in page "A"?

My goal would be for content in page "A" to be able to use the `\definiteintegral{}{}{}` macro even when it's never been defined directly in page "A".

Davide Cervone

unread,
Aug 27, 2025, 4:40:24 PMAug 27
to mathja...@googlegroups.com
Alex:

Without access to the outer frame, that may be difficult to accomplish.  Unless the contents of the two frames from the same domain, there are significant security restrictions, and the inner iframe can't access the outer frame at all without the cooperation of the outer frame.

If they are from the same domain, then you potentially could run commands in the outer frame's context, and use that to ask MathJax to look through the math that it has processed and look for \newcommand calls that you could then re-run in your iframe.  I'm not going to work anything out specifically for that unless you indicate that both frames are from the same domain.

You could try running

<script>console.log(Array.from(parent.MathJax.startup.document.math))</script>

in the iframe and see if you are able to do that.  If so, then we can look into getting the definitions from the TeX source.

Davide


--
You received this message because you are subscribed to the Google Groups "MathJax Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mathjax-user...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/mathjax-users/CA%2BR-jrc5GO4OVbx-DesX%3Dzj6cv8EPVR7_VTFhLfYXG2C3th2Gg%40mail.gmail.com.

Alex Jordan

unread,
Aug 29, 2025, 3:26:20 PMAug 29
to mathja...@googlegroups.com
I oversimplified before when I said I had no control over the outer page. The context is that the outer page is a PreTeXt book, and we are loading a WeBWorK/PG exercise in an iframe. As I sat to write this, I thought they will generally have two different domains (the domain for the PreTeXt HTML and the domain of the WeBWorK server). But I realized just now that actually this is not an iframe with a src attribute. It is an iframe with a srcdoc attribute. The outer PreTeXt page uses javascript to fetch some pretty raw processing from the WeBWorK server, and then builds its own srcdoc for the iframe. (This is mainly to help get styling for the problem that will match well with the ambient PreTeXt page.)

So I think yes, they have the same domain. Or rather it's moot because there isn't really a domain for the iframe at all since there is no src attribute.

I was able to follow your lead about Array.from(window.MathJax.startup.document.math). I can access:
Array.from(window.MathJax.startup.document.math)[0].math
where PreTeXt will reliably define macros in the 0th entry of that array. And then I can run that early in the srcdoc, and it works. Thanks for the help!






Reply all
Reply to author
Forward
0 new messages