js 动态创建<script></script>标签 并执行其标签中的内容

//获取head的标签
var head= document.getElementsByTagName('head')[0];
//创建script标签
var script= document.createElement('script');
//属性赋值
script.type= 'text/javascript';
//下面为必要操作 否则将不能使用script标签中的内容
script.onload = script.onreadystatechange = function() {
if (!this.readyState || this.readyState === "loaded" || this.readyState === "complete" ) {
script.onload = script.onreadystatechange = null;
} };
//添加src属性值
script.src= url;(url为你自己的添加js 文件的路径)
head.appendChild(script);


------------此处讨论跳入后台加载js文件的一种方式 (后台使用重定向)---------------
<script  type="text/javascript" src="http://localhost:8080/aaa/cc/yyxx/hj/getJsInformation.action"></script>

@RequestMapping(value = "/getJsInformation.action")
public String getJsInformation(HttpServletResponse response, HttpServletRequest request,@RequestParam HashMap<String, Object> map) {
List<HashMap<String, Object>> list = service.getJS(map);
return "redirect:"+"http://localhost:8080/upload/abc.js";//冲定向到你js文件全路径地址即可
}
 
posted @ 2020-06-10 15:05  颂先生  阅读(5314)  评论(1编辑  收藏  举报