[急][解决立马结贴]如何根据js动态加载不同的外部js

renyiqiu 2016-12-26 10:16:06
有这么一个需求,我需要根据不同设备在id为news_bottom的div上加载不同的外部js,比如PC端的话就加载a.js,wap端的话就加在b.js


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script class="jquery library" src="/js/sandbox/jquery/jquery-1.8.2.min.js" type="text/javascript"></script>
<title>演示代码</title>
</head>
<body>
<div id="news_bottom">

</div>
</body>
</html>


大概实现要求就是:

$(document).ready(function () {
if ($("#currentDevice").val() == "wap") {
loadScript("news_bottom", "http://xxx.com/a.js");
}
else
{
loadScript("news_bottom", "http://xxx.com/b.js");
}
});
//加载js
var loadScript=function(id,scriptLink)
{

var obj = document.getElementById(id);
var oScript = document.createElement("script");
oScript.type = "text/javascript";
oScript.src = scriptLink;
obj.appendChild(oScript);
}



请大家为我这个loadScript进行修改或者给我个新的解决方案,急急急,解决立马结贴
...全文
210 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
Go 旅城通票 2016-12-28
  • 打赏
  • 举报
回复
    <div id="news_bottom">
 if(移动端)document.write('<script src="移动端js路径"><\/script>')
else document.write('<script src="pc端js路径"><\/script>')
    </div>
renyiqiu 2016-12-28
  • 打赏
  • 举报
回复
我都忘了直接放到html里指定位置这简单的方法了,感谢楼上的几位帮忙,我太笨了,这么原始的方法就能解决了还在纠结怎么生成。
daswcszxw 2016-12-27
  • 打赏
  • 举报
回复
1,别写append了。 直接在模版写script 标签。 <script type="text/javascript" src="" id="#news_bottom"></script> 或者在if 的时候写document.write(<script src=‘....’)
renyiqiu 2016-12-27
  • 打赏
  • 举报
回复
引用 4 楼 Free_Wind22 的回复:

		$(function(){
			var url;
			if (navigator.userAgent.match(/(iPhone|iPod|Android|ios|SymbianOS|Windows Phone|BlackBerry)/i)) {
				url = 'http://xxx.com/a.js';
			}else{
				url = 'http://xxx.com/b.js';
			}
			$("<script>").attr("src", url).appendTo("#news_bottom");
		})
js能生成了,但解析不了呢,只add了文本
  • 打赏
  • 举报
回复
引用 3 楼 renyiqiu的回复:
[quote=引用 2楼圭宁的小小前端 的回复:]1,用window.screen.width判断设备屏幕大小 2,根据不同的屏幕大小,var s=document.createElement('script');s.src='url';s.async=true;document.head.appendChild(s); 应该可以了
我想要指定位置显示呢,不只是能加载出来[/quote] 那你就自己选位置append啊
2016-12-27
  • 打赏
  • 举报
回复

		$(function(){
			var url;
			if (navigator.userAgent.match(/(iPhone|iPod|Android|ios|SymbianOS|Windows Phone|BlackBerry)/i)) {
				url = 'http://xxx.com/a.js';
			}else{
				url = 'http://xxx.com/b.js';
			}
			$("<script>").attr("src", url).appendTo("#news_bottom");
		})
renyiqiu 2016-12-27
  • 打赏
  • 举报
回复
引用 2楼圭宁的小小前端 的回复:
1,用window.screen.width判断设备屏幕大小 2,根据不同的屏幕大小,var s=document.createElement('script');s.src='url';s.async=true;document.head.appendChild(s); 应该可以了
我想要指定位置显示呢,不只是能加载出来
  • 打赏
  • 举报
回复
1,用window.screen.width判断设备屏幕大小 2,根据不同的屏幕大小,var s=document.createElement('script');s.src='url';s.async=true;document.head.appendChild(s); 应该可以了
lovebaby 2016-12-27
  • 打赏
  • 举报
回复
记得jquery的AJAX也有一个加载JS文件的方法
2016-12-27
  • 打赏
  • 举报
回复

<div id="news_bottom">
	<script type="text/javascript">
	(function(){
		var url;
		if (navigator.userAgent.match(/(iPhone|iPod|Android|ios|SymbianOS|Windows Phone|BlackBerry)/i)) {
			url = 'http://xxx.com/a.js';
		}else{
			url = 'http://xxx.com/b.js';
		}
		document.write("<scr" + "ipt src='" + url + "' type='text/javascript'></scr" + "ipt>");
	})()
	</script>
</div>

87,910

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧