Home > Web Front-end > JS Tutorial > body text

How to prevent mobile browsers from displaying PC ads_javascript skills

WBOY
Release: 2016-05-16 15:36:34
Original
1764 people have browsed it

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'
Copy after login

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)
Copy after login

iphone device userAgent judgment

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

ipad device userAgent judgment

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

ipod device userAgent judgment

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

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;
    }
 }
Copy after login

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> //广告内容加载
Copy after login

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.

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template