Home > Web Front-end > JS Tutorial > Combining arrays with strings in js to implement search (blocking advertising and judging URLs, etc.)_javascript skills

Combining arrays with strings in js to implement search (blocking advertising and judging URLs, etc.)_javascript skills

WBOY
Release: 2016-05-16 15:07:07
Original
2739 people have browsed it

1. Ad blocking

Sometimes our advertising pages are uniformly controlled with js. Some pages do not want to display some ads, so we only need the id of the article. Pure string search is simple and effective. Script Home also uses it

var ad_softlist = ',,133015,155868,146429,';
if("undefined" != typeof softid){ //判断文章id是否存在,不存在就不执行,一般页面中会定义好var softid=45465;
if(softid!=null && ad_softlist.indexOf(','+softid+',')==-1){
//显示广告,softid就是文章id
}
Copy after login

2. Determine whether the URL conforms to simple rules

Combine arrays and strings, because judgment requires two parts, using arrays makes the structure simple

It turns out that we always have URL errors during the update process:
For example: http://http://www.jb51.net

htp:// and other error conditions. So we have written down all the commonly used URLs. See the code for details.

function checkurl(urls){
if(urls!=""){
if((urls.indexOf('http://http')!=-1) || urlcheck(urls)!=true ){
	alert("网址有问题吧,可以再检查一下刚加的网址");
}
}
}

function urlcheck(str){
var urlall="http://,https://,ed2k://,thunder://,flashget://,ftp://";
var urlarr=urlall.split(",");
for(var i=0;i<urlarr.length;i++){
	if(str.indexOf(urlarr[i])>-1){
		return true;
		}
}
return false;
}
Copy after login

How to use:

I am afraid that the data cannot be submitted due to accidental killing. It is just a friendly reminder and I did not write the form to check the return.

Original article by Script House, please sign

for reprinting
Related labels:
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