1

I'm writing an extension and I want part of the code to only run in iframes. Currently, all the code in my extensuin.js file runs in the page and iframes How can I detect that the code is running in an iframe so that I can run iframe specific code?

Just as an example (this is not real code) my extension.js should be something like:

appAPI.ready(function($) {
  if (iframe) {
    // Run iframe code
  } else {
    // Run on regular page
  }
});
skuntsel
  • 11,624
  • 11
  • 44
  • 67
dario
  • 11
  • 1
  • Possible duplicate: http://stackoverflow.com/questions/4594492/to-check-parent-window-is-iframe-or-not – Inferpse Oct 15 '14 at 08:43
  • @Inferpse Your comment isn't actually correct as the responses in the ticket you cited would not work in all cases when run the context of the extension. See my answer for the correct approach. [**Disclosure**: I am a Crossrider employee] – Shlomo Oct 15 '14 at 09:00

1 Answers1

2

You can achieve your goal by using the appAPI.dom.isFrame to determine if your extension code is running in an iframe. So, using your example, the code would be:

appAPI.ready(function($) {
  if (appAPI.dom.isFrame()) {
    // Run iframe code
  } else {
    // Run on regular page
  }
});

[Disclosure: I am a Crossrider employee]

Shlomo
  • 3,763
  • 11
  • 16