dpcwz210393 2013-07-04 15:33
浏览 129
已采纳

使用document.write在PHP中编写JS

This is failing but i dont know how to make it work.

I have a php array:

$swipe_list = array('customers.html', 'index.html', 'atr_backplane.html');

This is to know which pages have carousels and need touch support.

so i check the page in php and if its listed the JS will check its a touch device and write the addional js mobile file.

is it because the server script trys ti run before the js can detect at the client side??

<?php
if(in_array($selected, $swipe_list)){ ?>
<script type="text/javascript">

function isTouchDevice(){ document.write('<script type="text/javascript" src="js/jquery.mobile.custom.min.js"></script>')
}

</script>            
<?php }?>
<?php if(!in_array($selected, $exclude_list)){       
print '<script type="text/javascript" src="js/camera.js"></script>';
}
?>
  • 写回答

3条回答 默认 最新

  • doulu2591 2013-07-04 15:42
    关注

    When you use document.write after the page has loaded, all kinds of bad things can happen. I don't remember the exact reason why, but I know it breaks the DOM somehow.

    The best thing would be to include the script dynamically without breaking the DOM using something like this instead:

    function isTouchDevice(){
        var script = document.createElement('script');
        script.type = 'text/javascript';
        script.src = 'js/jquery.mobile.custom.min.js';
        document.getElementsByTagName('head')[0].appendChild(script);
    }
    

    This creates the script element and appends it to the head of the document.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器