Home  >  Article  >  Web Front-end  >  js中数组结合字符串实现查找(屏蔽广告判断url等)_javascript技巧

js中数组结合字符串实现查找(屏蔽广告判断url等)_javascript技巧

WBOY
WBOYOriginal
2016-05-16 15:07:072675browse

1、广告屏蔽

有时候我们的广告页面都是统一的用js控制的,有些页面不想显示部分广告,那么我们只需要文章的id即可,纯字符串查找简单有效,脚本之家也在用

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

2、判断url是否符合简单的规则

结合数组与字符串,因为判断需要两个部分,使用数组使结构变得简单

原来我们在更新过程中总会出现网址错误的情况:
例如:http://http://www.jb51.net

htp://等错误情况。所以我们把常用的几种网址都写了下来。具体看代码。

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;
}

使用方法:

我是为了怕误杀造成数据无法提交,只作为友情提醒,没写入form检查返回。

脚本之家原创文章,转载请署名

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