如何在运行时将javascript代码加载到html文件? [英] How to load javascript code to an html file at runtime?

查看:40
本文介绍了如何在运行时将javascript代码加载到html文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我问一下是否可以在运行时将javascript代码加载到html文件中.例如,放置一个文本框以输入脚本文件的位置,并放置一个表单按钮以触发加载脚本文件.

Let me ask whether it is possible to load javascript code to an html file at runtime. For example , place a textbox to input the location of the script files and a form button to trigger loading the script files.

谢谢.

推荐答案

将其粘贴在该按钮的onclick内(更正第三行中的网址):

Paste this within onclick of that button (correct the url in 3rd line):

var script = document.createElement("script");
script.setAttribute("type", "text/javascript");
script.setAttribute("src", "url to the script file here");
document.getElementsByTagName("head")[0].appendChild(script);

该脚本将在执行第4行后立即开始下载,下载后即会执行或可以使用.

That script will start being downloaded immediately after line 4 is executed, and as soon as it's downloaded it'll execute or otherwise be usable.

这篇关于如何在运行时将javascript代码加载到html文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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