如何使移动设备不加载外部JS [英] How to make mobile devices not load an external JS

查看:116
本文介绍了如何使移动设备不加载外部JS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个我接手的网站即将推出。该网站有一个脚本,用于加载每个页面的ajax风格,并更新能够连续播放音乐播放器的URL。这一切在桌面浏览器上都非常棒。但是,这一功能正在使iPhone和iPad上的Safari崩溃。

如果不是移动设备或平板电脑,我该如何编写一些Javascript来加载外部JS文件?即iPhone和iPad。我知道上的资源,尽管我对JavaScript的了解还很少利用这个脚本来满足我的需求。

无论我能做什么来使iPhones和iPad不加载特定的JS文件,(现在)都可以解决这个问题。有任何想法吗?不幸的是,服务器端解决方案是不可能的。



这是我用jquery简单检测Mobile的结果:

  / ** 
* jQuery.browser.mobile(http://detectmobilebrowser.com/)
*
* jQuery.browser.mobile将为true,如果浏览器是移动设备
*
** /
(function(a){(jQuery.browser = jQuery.browser || {})。mobile = /(android | bb\d + | meego)。+ mobile | avantgo | bada \ / | blackberry | blazer | compal | elaine | fennec | hiptop | iemobile | ip(hone | od)| iris | kindle | lge | maemo | midp | mmp | netfront | opera m(ob | in )i | palm(os)?| phone | p(ixi | re)\ / | plucker | pocket | psp | series(4 | 6)0 | symbian | treo | up \。(browser | link)| vodafone | wap | windows(ce | phone)| xda | xiino / i.test(a)|| / 1207 | 6310 | 6590 | 3gso | 4thp | 50 [1-6] i | 770s | 802s | a wa | abac | AC(ER | OO | s\-)| AI(KO | RN)|人(AV | CA |共)|夏新|一(前|纽约| YW)| APTU | AR(CH |去)|为( (c | | n |)| bi(lb | rd)| bl(ac | az)| br(e | v)的W | bumb | bw\​​-(N | U)| c55\ / | CAPI | CCWA | cdm\- |细胞| CHTM | CLDC | cmd\- |ω(MP | ND)|嗉| DA(它| LL | NG)| dbte | dc\\ \\-S |德维| DICA | dmob |做(C | p)的直径:| DS(12 | \-d)| EL(49 | AI)| EM(L2 | UL)| ER(IC | K0)| esl8 | | ez([4-7] 0 | os | wa | ze | fetc | fly(\\ - - | _)| g1 u | g560 | gene | gf\\-5 | g \ -mo | (h | h)| hd | - (m | p | t)| hei | - | hi(pt | ta)| hp(i | ip)| hs \-C | HT(C(\- | (20 | go | ma)| i230 | iac(| \- | \ /)| |(| | | | | | | | | | | ibro | idea | ig01 | ikom | im1k | inno | ipaq | iris | ja(t | v)a | jbro | jemu | jigs | kddi | keji | kgt(| \ /)| klon | kpt | kwc\- | |(| | | | | | | | W |空间M3GA | m50\ / | MA(TE | UI | XO)| MC(01 | 21 | CA)| m\-CR |我(RC | RI)|英里(O8 | OA | TS)| MMEF | mo(01 | 02 | bi | de | do | t(\- | | o | v)| zz)| mt(50 | p1 | v)| mwbp | mywa | n10 [0-2] | n20 [ 2-3] | N30(0 | 2)| N50(0 | 2 | 5)| N7(0(0 | 1)| 10)| NE((C | M)\- |在| TF | WF | WG |重量)| NOK(6 | I)| nzph | o2im | OP(TI | WV)|奥兰| owg1 | P800 |锅(A | d | T)| pdxg | PG(13 | \ - ([1 -8] | C))|菲尔| PIRE | PL(AY | UC)| pn\-2 | PO(CK |室温| SE)| PROX | PSIO | pt\-G | qa\-A | QC(07 | 12 | 21 | 32 | 60 | \- [2-7] | i\-)| QTEK | R380 | R600 | RAKS | rim9 | RO(VE | ZO)| s55\ / | SA (GE |毫安|毫米|毫秒| NY | VA)| SC(01 | h\- | oo的| p\-)| sdk\ / | SE(C(\- | 0 | 1)| 47 | MC | ND | RI)| sgh\- |沙皮| SIE(\- | M)| sk\-0 | SL(45 | ID)| SM(AL | AR | B3 |它| T5)| so(01 | mb)| t2(18 | 50)| t6(00 | 10 | 18)| ta(gt | | | | | | | v | | LK)| tcl\- | TDG \\- |电话(I | M)| tim\- | t\谟|至(PL | SH)| TS(70 | m\- |立方米| M5)| tx\-9 |向上( \.b | G1 | SI)| utst | V400 | V750 | VERI | VI(RG | TE)| VK(40 | 5 [0-3] | \-v)| vm40 |沃达| vulc | VX( 52 | 53 | 60 | 61 | 70 | 80 | 81 | 83 | 85 | 98)| W3C(\- | )| webc | whit | wi(g | nc | nw)| wmlb | wonu | x700 | yas\- | your | zeto | zte\- / i.test(a.substr(0,4))}) (navigator.userAgent的|| || navigator.vendor window.opera);


解决方案

为什么后端解决方案不适用?



无论如何,请确保您包含jQuery和Detect Mobile(jQuery):

 < HEAD> 
< script src =jquery.js>< / script>
< script src =detectmobilebrowser.js>< / script>
< / head>

现在检测它是否不是手机:

  if(!jQuery.browser.mobile){
//创建脚本元素
}

如何创建脚本元素?

  var脚本= document.createElement('script'); 
script.type ='text / javascript';
script.src ='http:// URLTOSCRIPT';

$('body')。append(script);


I have a website I've taken over which is nearing launch. The website has a script which loads each page ajax style and updates the URL which enables a continuously playing music player. It all works fantastic on desktop browsers. However, this same feature is crashing Safari on the iPhone and iPad.

How exactly could I write a bit of Javascript to load an external JS file if it is NOT mobile or a tablet? Namely, iPhone and iPad. I'm aware of the resource at http://detectmobilebrowsers.com/, though my knowledge of Javascript is just short of utilizing this script to meet my needs.

Whatever I can do to make iPhones and iPads not load a specific JS file would (for now) fix this issue. Any ideas? Unfortunately, server side solutions are just not possible.

Here is what I got to simply detect Mobile with jquery:

/**
 * jQuery.browser.mobile (http://detectmobilebrowser.com/)
 *
 * jQuery.browser.mobile will be true if the browser is a mobile device
 *
 **/
(function(a){(jQuery.browser=jQuery.browser||{}).mobile=/(android|bb\d+|meego).+mobile|avantgo|bada\/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)\/|plucker|pocket|psp|series(4|6)0|symbian|treo|up\.(browser|link)|vodafone|wap|windows (ce|phone)|xda|xiino/i.test(a)||/1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s\-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|\-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw\-(n|u)|c55\/|capi|ccwa|cdm\-|cell|chtm|cldc|cmd\-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc\-s|devi|dica|dmob|do(c|p)o|ds(12|\-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(\-|_)|g1 u|g560|gene|gf\-5|g\-mo|go(\.w|od)|gr(ad|un)|haie|hcit|hd\-(m|p|t)|hei\-|hi(pt|ta)|hp( i|ip)|hs\-c|ht(c(\-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i\-(20|go|ma)|i230|iac( |\-|\/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |\/)|klon|kpt |kwc\-|kyo(c|k)|le(no|xi)|lg( g|\/(k|l|u)|50|54|\-[a-w])|libw|lynx|m1\-w|m3ga|m50\/|ma(te|ui|xo)|mc(01|21|ca)|m\-cr|me(rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(\-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)\-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|\-([1-8]|c))|phil|pire|pl(ay|uc)|pn\-2|po(ck|rt|se)|prox|psio|pt\-g|qa\-a|qc(07|12|21|32|60|\-[2-7]|i\-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55\/|sa(ge|ma|mm|ms|ny|va)|sc(01|h\-|oo|p\-)|sdk\/|se(c(\-|0|1)|47|mc|nd|ri)|sgh\-|shar|sie(\-|m)|sk\-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h\-|v\-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl\-|tdg\-|tel(i|m)|tim\-|t\-mo|to(pl|sh)|ts(70|m\-|m3|m5)|tx\-9|up(\.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|\-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(\-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|yas\-|your|zeto|zte\-/i.test(a.substr(0,4))})(navigator.userAgent||navigator.vendor||window.opera);

解决方案

Why is a backend solution not applicable?

Anyway, make sure you include jQuery and the Detect Mobile (jQuery):

<head>
    <script src="jquery.js"></script>
    <script src="detectmobilebrowser.js"></script>
</head>

Now to detect if it's NOT a mobile:

if ( !jQuery.browser.mobile ) {
    // create the script element
}

How do you create the script element?

var script = document.createElement('script');
script.type = 'text/javascript';
script.src = 'http://URLTOSCRIPT';

$('body').append(script);

这篇关于如何使移动设备不加载外部JS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆