1

I'm trying to include an external script in my react component in Meteor. I've attempted just placing the script tag in my component like so :

TheLounge = React.createClass({
  render() {
    return (
      <div>
        <div className="main">
          {/* Uberflip Embedded Hub (Use this to generate the frame below) */}
          <script>var ufh_top_offset = 0, ufh_bottom_offset = 0;</script>
          <script type="text/javascript" src="https://thelounge.tullyluxurytravel.com/hubsFront/embed_iframe?u=https%3A%2F%2Fthelounge.tullyluxurytravel.com%2Fh%2F%3Fembedded%3D1%26offsetTop%3D0%26offsetBottom%3D0%26hideHeader%3D1%26hideBanner%3D0%26hideFooter%3D1%26hidePriNav%3D0%26hideSecNav%3D0%26linkBreakOut%3D0"></script>
          {/* /End Uberflip Embedded Hub */}

        </div>
        <Footer />
      </div>
    );
  },
});

This makes it fine for the first time the page is loaded (via another page or a refresh). However, the script doesn't load up again when one navigates to another page and back.

So far I've attempted the following:

  • Injecting the script in componentDidMount and componentDidUpdate like the following:

In componentDidMount and componentDidUpdate:

var script = document.createElement("script");
script.type = "text/javascript";
script.src = url;
// Tested the line below with body, head, and a div with specific id.
document.getElementsByTagName("body")[0].appendChild(script);

I've also tried using these packages:

However, with all four of the approaches above I get the error:

Error: Failed to execute 'write' on 'Document': It isn't possible to write into a document from an asynchronously-loaded external script unless it is explicitly opened.

I realize that's because the script I'm trying to load uses document.write();

I'm running out of things to try, I'd appreciate new ideas as to how to include this script into the React component.

2 Answers 2

1

tags in body or templates aren't executed by Meteor, they are parsed and then handled by Meteor's templating system. try jQuery getScript function:

componentWillMount() {
  $.getScript('https://thelounge.tullyluxurytravel.com/hubsFront/embed_iframe?u=https%3A%2F%2Fthelounge.tullyluxurytravel.com%2Fh%2F%3Fembedded%3D1%26offsetTop%3D0%26offsetBottom%3D0%26hideHeader%3D1%26hideBanner%3D0%26hideFooter%3D1%26hidePriNav%3D0%26hideSecNav%3D0%26linkBreakOut%3D0');
}
Sign up to request clarification or add additional context in comments.

1 Comment

Note that this will never cache the script
0

I didn't try this out, but I read this for blaze. Maybe it can helps

https://maxlibin.com/meteor-include-external-javascript-files/

Comments

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.