js操作cookies增删查操作
function GetCookieDomain ( ) {
var host = location. hostname;
var ip = /^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/ ;
if ( ip. test ( host) === true || host === 'localhost' ) return host;
var regex = /([^]*).*/ ;
var match = host. match ( regex) ;
if ( typeof match !== "undefined" && null !== match) host = match[ 1 ] ;
if ( typeof host !== "undefined" && null !== host) {
var strAry = host. split ( "." ) ;
if ( strAry. length > 1 ) {
host = strAry[ strAry. length - 2 ] + "." + strAry[ strAry. length - 1 ] ;
}
}
return '.' + host;
}
function setCookie ( cname, cvalue, exdays = 0 ) {
cvalue = encodeURIComponent ( JSON . stringify ( cvalue) ) ;
if ( exdays > 0 ) {
var d = new Date ( ) . getTime ( ) + exdays * 24 * 3600 * 1000 + 8 * 3600 * 1000 ;
var expires = "expires=" + new Date ( d) . toUTCString ( ) ;
document. cookie = cname + "=" + cvalue + ";" + expires + ";path=/" ;
} else {
document. cookie = cname + "=" + cvalue + ";" + ";path=/" ;
}
}
function getCookie ( name) {
var arr, reg= new RegExp ( "(^| )" + name+ "=([^;]*)(;|$)" ) ;
if ( arr= document. cookie. match ( reg) ) {
return unescape ( arr[ 2 ] ) ;
} else {
return null ;
}
}
function delCookie ( name) {
var date = new Date ( ) ;
date. setTime ( date. getTime ( ) - 1 ) ;
var delValue = getCookie ( name) ;
if ( delValue) {
document. cookie = name + "=" + delValue + ";expires=" + date. toGMTString ( ) ;
}
}
封装动画函数
1. 向下滚动
function idxAnimate ( obj, target) {
clearInterval ( goHeightTimer)
let nowPosition1 = null ;
goHeightTimer = setInterval ( function ( ) {
nowPosition1 = parseInt ( window. pageYOffset)
let step = ( target - nowPosition1) / 10
step = step >= 0 ? Math. ceil ( step) : Math. floor ( step)
isTopHeight = true
if ( nowPosition1 == target) {
clearInterval ( goHeightTimer)
}
if ( step== 1 ) {
step = 0
nowPosition1 = target
}
document. documentElement. scrollTop= nowPosition1 + step;
} , 15 )
}
function scrollChangeHeader ( ) {
if ( ! isTop) {
clearInterval ( goToptimer) ;
}
isTop = false ;
}
2. 回滚到顶部
goTop. addEventListener ( 'click' , function ( ) {
goToptimer = setInterval ( function ( ) {
let osTop = document. documentElement. scrollTop || document. body. scrollTop;
let speed = Math. floor ( - osTop / 6 ) ;
document. documentElement. scrollTop = document. body. scrollTop = osTop + speed;
isTop = true ;
if ( osTop == 0 ) {
clearInterval ( goToptimer) ;
}
} , 30 ) ;
header. className = 'index-header'
initTop ( )
} , false )
3. swiper横向滚动
function swiperAnimate ( obj, target) {
clearInterval ( obj. timer)
obj. timer = setInterval ( function ( ) {
let nowPosition = null ;
let offsetLeft = parseInt ( obj. offsetLeft)
nowPosition = offsetLeft
let step = ( target - nowPosition) / 10
step = step > 0 ? Math. ceil ( step) : Math. floor ( step)
if ( nowPosition == target) {
clearInterval ( obj. timer)
}
obj. style. left = nowPosition + step + 'px' ;
} , 15 )
}
js获取url参数
function getQueryString ( name) {
let reg = new RegExp ( '(^|&)' + name + '=([^&]*)(&|$)' , 'i' ) ;
let r = window. location. search. substr ( 1 ) . match ( reg) ;
if ( r != null ) {
return unescape ( r[ 2 ] ) ;
}
return null ;
}
封装jsonp
var ajax = function ajax ( params) {
params = params || { } ;
params. data = params. data || { } ;
jsonp ( params) ;
function jsonp ( params) {
var callbackName = params. jsonp;
var head = document. getElementsByTagName ( 'head' ) [ 0 ] ;
params. data[ 'callback' ] = callbackName;
var data = formatParams ( params. data) ;
var script = document. createElement ( 'script' ) ;
head. appendChild ( script) ;
script. src = params. url + '?' + data;
window[ callbackName] = function ( json) {
head. removeChild ( script) ;
clearTimeout ( script. timer) ;
window[ callbackName] = null ;
params. success && params. success ( json) ;
} ;
if ( params. time) {
script. timer = setTimeout ( function ( ) {
window[ callbackName] = null ;
head. removeChild ( script) ;
params. error && params. error ( {
message: '超时'
} ) ;
} , time) ;
}
} ;
function formatParams ( data) {
var arr = [ ] ;
for ( var name in data) {
arr. push ( encodeURIComponent ( name) + '=' + encodeURIComponent ( data[ name] ) ) ;
}
arr. push ( 'v=' + random ( ) ) ;
return arr. join ( '&' ) ;
}
function random ( ) {
return Math. floor ( Math. random ( ) * 10000 + 500 ) ;
}
} ;
ajax ( {
"url" : jsonpurl,
"jsonp" : "success_jsonpCallback" ,
"data" : {
token: mytoken
} ,
success: function ( res) {
console. log ( '后台没有提供回调函数,默认成功' )
if ( res != null ) {
console. log ( "跨域请求成功!" ) ;
}
} ,
error: function ( error) {
}
} ) ;
封装ajax
function newAjax ( method, url, data, callback, type) {
let xhr;
if ( window. XMLHttpRequest) {
xhr = new XMLHttpRequest ( ) ;
} else {
xhr = new ActiveXObject ( "Microsoft.XMLHTTP" ) ;
}
xhr. open ( method, url, true ) ;
let param = '' ;
if ( JSON . stringify ( data) != '{}' ) {
url += '?' ;
for ( let i in data) {
param += i + '=' + data[ i] + '&' ;
}
param = param. slice ( 0 , param. length - 1 ) ;
}
if ( method == "get" ) {
url = url + param;
}
if ( method == "post" ) {
type == 'json' ? xhr. setRequestHeader ( "Content-Type" , "application/json; charset=utf-8" ) : xhr. setRequestHeader ( "Content-Type" , "application/x-www-form-urlencoded" ) ;
type == 'json' ? xhr. send ( JSON . stringify ( data) ) : xhr. send ( data)
} else {
xhr. send ( null ) ;
}
xhr. onreadystatechange = function ( ) {
let res;
if ( xhr. status == 200 && xhr. readyState == 4 ) {
if ( type == 'json' ) {
res = JSON . parse ( xhr. responseText) ;
} else if ( type == 'xml' ) {
res = responseXML;
} else {
res = xhr. responseText;
}
callback ( res) ;
}
} ;
}