document.write - javascript

document.write - javascript

本文关键字:javascript write document      更新时间:2023-09-26

我想根据设备类型(移动/桌面)加载资源,我正在使用JS函数来检测设备的类型,并使用document.write在HTML中写入资源。此代码位于页面的<head>中。

<script type='text/javascript'>
    if (!is_mobile()) {
        document.writeln('<script type="text/javascript" src="/etc/designs/blabla-poc/js/bootstrap.min.js"><'/script>');
    }else{
        document.writeln('<script type="text/javascript" src="js/bootstrap.min.js"><'/script>');
    }
</script>

但它在代码中写入document.writeln('<script src="js/bootstrap.min.js"><'/script>');而不是<script src="js/bootstrap.min.js"></script>

我做错了什么?

标签拆分为更多字符串,以防止浏览器错误地理解它们

document.writeln('<scr' + 'ipt ... ></scr' + 'ipt>');

更新:

  • 破碎:http://demo.chobits.ch/js/include/index.html
  • 固定:http://demo.chobits.ch/js/include/fixed.html