为什么要添加< script>运行时标记不加载javascript文件? (with react.js) [英] Why adding a <script> tag at runtime doesn't load the javascript file? (with react.js)

查看:64
本文介绍了为什么要添加< script>运行时标记不加载javascript文件? (with react.js)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个react.js脚本,它将以下代码添加到html

I have this react.js script that adds the following code into the html

// returned by the render method
React.DOM.div({
    dangerouslySetInnerHTML:  {
        __html: '<script type="text/javascript" async="" src="//myapp.disqus.com/embed.js"></script>'
    }
})

现在我的html看起来像:

Now my html looks like:

<script type="text/javascript" async="" src="//myapp.disqus.com/embed.js"></script>

这看起来很完美,但问题是它没有加载脚本。
脚本标签插入到正文的中间,嵌套在其他一些div标签中。

Which seems perfect but the problem is that it doesn't load the script. The script tag is inserted into the middle of the body, nested within some other div tags.

可能是什么问题?
谢谢

推荐答案

将脚本标签渲染到带反应的页面不是正确的解决方案 - 我coudln'让它与JSX一起工作,我认为这同样适用。不知道为什么,但只是添加普通的旧javascript方式:

Rendering the script tag to the page with react isn't the right solution - I coudln't get it to work with JSX, I assume the same applies here. Not sure why, but just add it the plain old javascript way:

    var script = document.createElement("script");

    script.src = "//myapp.disqus.com/embed.js";
    script.async = true;

    document.body.appendChild(script);

将它放在根组件的componentWillMount中。

Put that in the componentWillMount of your root component.

这篇关于为什么要添加&lt; script&gt;运行时标记不加载javascript文件? (with react.js)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆