I am having problem loading a script from within React side
This is the script:
<script>!function(e,t,s,i){var n="InfogramEmbeds",o=e.getElementsByTagName("script")[0],d=/^http:/.test(e.location)?"http:":"https:";if(/^\/{2}/.test(i)&&(i=d+i),window[n]&&window[n].initialized)window[n].process&&window[n].process();else if(!e.getElementById(s)){var r=e.createElement("script");r.async=1,r.id=s,r.src=i,o.parentNode.insertBefore(r,o)}}(document,0,"infogram-async",".js");</script>
I tried something like this:
const script = document.createElement("script");
script.src = scriptUrl;
script.charset = "utf-8";
script.async = true;
script.onload = function() {
!function(e,t,s,i){var n="InfogramEmbeds",o=e.getElementsByTagName("script")[0],d=/^http:/.test(e.location)?"http:":"https:";if(/^\/{2}/.test(i)&&(i=d+i),window[n]&&window[n].initialized)window[n].process&&window[n].process();else if(!e.getElementById(s)){var r=e.createElement("script");r.async=1,r.id=s,r.src=i,o.parentNode.insertBefore(r,o)}}(document,0,"infogram-async",".js");
};
document.body.appendChild(script);
If I add the script directly into header than it works perfectly fine.
I am having problem loading a script from within React side
This is the script:
<script>!function(e,t,s,i){var n="InfogramEmbeds",o=e.getElementsByTagName("script")[0],d=/^http:/.test(e.location)?"http:":"https:";if(/^\/{2}/.test(i)&&(i=d+i),window[n]&&window[n].initialized)window[n].process&&window[n].process();else if(!e.getElementById(s)){var r=e.createElement("script");r.async=1,r.id=s,r.src=i,o.parentNode.insertBefore(r,o)}}(document,0,"infogram-async","https://e.infogram./js/dist/embed-loader-min.js");</script>
I tried something like this:
const script = document.createElement("script");
script.src = scriptUrl;
script.charset = "utf-8";
script.async = true;
script.onload = function() {
!function(e,t,s,i){var n="InfogramEmbeds",o=e.getElementsByTagName("script")[0],d=/^http:/.test(e.location)?"http:":"https:";if(/^\/{2}/.test(i)&&(i=d+i),window[n]&&window[n].initialized)window[n].process&&window[n].process();else if(!e.getElementById(s)){var r=e.createElement("script");r.async=1,r.id=s,r.src=i,o.parentNode.insertBefore(r,o)}}(document,0,"infogram-async","https://e.infogram./js/dist/embed-loader-min.js");
};
document.body.appendChild(script);
If I add the script directly into header than it works perfectly fine.
Share Improve this question asked Mar 12, 2019 at 15:05 MizlulMizlul 2,29010 gold badges52 silver badges105 bronze badges2 Answers
Reset to default 3All you need to do is call the function inside ponentDidMount and append the child into head like this
ponentDidMount() {
const script = document.createElement("script");
script.src = 'https://www.google.'; // whatever url you want here
script.charset = "utf-8";
script.async = true;
script.onload = function () {
!function (e, t, s, i) { var n = "InfogramEmbeds", o = e.getElementsByTagName("script")[0], d = /^http:/.test(e.location) ? "http:" : "https:"; if (/^\/{2}/.test(i) && (i = d + i), window[n] && window[n].initialized) window[n].process && window[n].process(); else if (!e.getElementById(s)) { var r = e.createElement("script"); r.async = 1, r.id = s, r.src = i, o.parentNode.insertBefore(r, o) } }(document, 0, "infogram-async", "https://e.infogram./js/dist/embed-loader-min.js");
};
document.head.appendChild(script);
}
I have created an npm package, a react hook to load a script dynamically. You can use that to load script dynamically in your react application. It will add script in the head
or as child of any specified HTML element.
Here is the link to the package https://www.npmjs./package/use-script-loader
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744976273a4604163.html
评论列表(0条)