Home >Web Front-end >JS Tutorial >How to prevent mobile browsers from displaying PC ads_javascript skills

How to prevent mobile browsers from displaying PC ads_javascript skills

WBOY
WBOYOriginal
2016-05-16 15:36:341782browse

I wrote a blog post some time ago about WeChat’s built-in browser not displaying affiliate ads

Determine whether the current browser is WeChat’s built-in browser based on window.navigator.userAgent

window.navigator.userAgent=='micromessenger'

The content I want to share today is almost the same, because I only considered the adaptation of WeChat browser before. When I opened the site on the mobile browser, I found the adaptation problem.
Some alliances will automatically block the mobile version, but some alliances cannot block it, so you can only judge manually.

Determine the source based on the browser userAgent

UserAgent judgment for Android devices

navigator.userAgent.match(/Android/i)

iphone device userAgent judgment

navigator.userAgent.indexOf('iPhone')!=-1

ipad device userAgent judgment

navigator.userAgent.indexOf('iPad') != -1

ipod device userAgent judgment

navigator.userAgent.indexOf('iPod') != -1

Add more How to judge WeChat’s built-in browser

function isWeiXin() {
    var ua = window.navigator.userAgent.toLowerCase();
    if (ua.match(/MicroMessenger/i) == 'micromessenger') {
      return true;
    } else {
      return false;
    }
 }

Block WeChat, mobile phones (except winphone devices), and iPod

 if(!isWeiXin()&&!(navigator.userAgent.match(/Android/i) || (navigator.userAgent.indexOf('iPhone') != -1) || (navigator.userAgent.indexOf('iPod') != -1) || (navigator.userAgent.indexOf('iPad') != -1))) {
     var sogou_ad_id=4767753;  //联盟广告计费id
 }
<script src="http://images.sohu.com/cs/jsfile/js/c.js" charset="utf-8"></script> //广告内容加载

If there is no affiliate advertising billing ID, the following advertisements will naturally not be displayed. This indirectly plays the role of blocking affiliate advertisements on the mobile terminal.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn