如果用户的设备是移动设备,如何停止特定的外部javascript文件

How to stop specific external javascript file if the user's device is mobile device

本文关键字:外部 javascript 何停止 文件 用户 移动 如果      更新时间:2023-09-26

我想在我的网站上放一个广告,广告是弹出在广告下。但有时在移动设备上它会弹出。我想在移动设备上停止这种广告代码,允许它出现在桌面电脑上。我不懂javascript。我试过了,但没有用。因为我认为把javascript内javascript是不合法的。我应该怎么做才能在移动设备上停止特定的脚本?这是我的代码

<script type="text/javascript"> 
    function isMobile() {
        return /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent);
    }
    if (!isMobile()) {
       <script type="text/javascript" src="Ad company URL Here"></script>
    }
</script>

你必须把append这个新的script元素放到document

if (!isMobile()) {
   var src = "Ad company URL Here"; //Replace this with the, you know, actual source
   var scriptElem = document.createElement( 'script' );
   scriptElem.setAttribute('src', src);
   document.body.appendChild(scriptElem);
}