Dynamically adding script in html, not working | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Dynamically adding script in html, not working

I want to add a script at the end of a tag but it is not working. Here I have already added script so it's working. https://code.sololearn.com/W5A4a14A7a38/?ref=app Here I have tried to add script later but it's not working. https://code.sololearn.com/Wa40a24A03A1/?ref=app (desktop)

23rd Jan 2021, 4:32 PM
harshit
harshit - avatar
6 Answers
+ 2
harshit You can try like this function addScript( src ) { var s = document.createElement( 'script' ); s.setAttribute( 'src', src ); document.body.appendChild( s ); } document.addEventListener("DOMContentLoaded", function(event) { addScript ("https://platform.twitter.com/widgets.js"); });
23rd Jan 2021, 6:55 PM
A͢J
A͢J - avatar
0
I Am AJ ! Do you know why my code is not working, it was working with normal html but not with script.
23rd Jan 2021, 7:20 PM
harshit
harshit - avatar
0
harshit On Sololearn I don't think dynamic script will work but on desktop browsers sure it will work because we are already doing like that.
23rd Jan 2021, 7:22 PM
A͢J
A͢J - avatar
0
I Am AJ ! This is not working in desktop also. https://code.sololearn.com/Wa40a24A03A1/?ref=app
23rd Jan 2021, 7:33 PM
harshit
harshit - avatar
0
Btw. the problems seems to be that Sololearn interprets the JS as HTML if it includes a script tag for some reason...
23rd Jan 2021, 7:49 PM
Aaron Eberhardt
Aaron Eberhardt - avatar
0
Aaron Eberhardt it is same with locally also.
23rd Jan 2021, 8:29 PM
harshit
harshit - avatar