// JavaScript Document
/**
* 网站全局要引用的js
* heyuejuan
**/
// heyuejuan function 的封装
/*================== 常用对象 =====================*/
$1 = {
testfun:function(val){alert(val);},
get_id:function(id){ // 获得 id 对象
return document.getElementById(id);
},
get_name:function(name){ // 获得 name 对象
return document.getElementsByName(name);
},
get_elem:function(element){ // 获得元素对象
return document.getElementsByTagName(element);
},
gb:function(){ // IE 清除内存
if(isIE){
try{
CollectGarbage();
}catch(e){}
}
},
rand:function(num){ // 生成一个随机数 最大值 生成 0-num 之间的值
return this.cint(num*Math.random());
},
trim:function(str){ // 去掉前后空格
if(!str) return '';
if(str==undefined) return '';
if( ! isNaN(str) ) return str;
return str.replace(/(^\s*)|(\s*$)/g, "");
},
cint:function(value){ // parseInt 转成数字 整型
if( (!value)) return 0;
var number = parseInt(value,10);
if(isNaN(number)) return 0;
return number;
},
cNumber:function(value){ // 转成数字型 整型与浮点型
if( (!value)) return 0;
var number = Number(value);
if(isNaN(number)) return 0;
return number;
},
cabs:function(value){ // 取整
number = this.cNumber(value);
number = Math.abs(number);
return number;
},
__eval:function(rest){ // 解析 json 成数组 返回数组 或 执行
try{
var obj = eval("(" + rest + ")");
}catch(e){
var obj = rest;
}
return obj;
},
is_email:function(email){ // 检测 email
var myreg = /^[_.0-9a-zA-Z-]+@([0-9a-zA-Z-]+\.)+[a-zA-Z]{2,3}$/;
return myreg.test(email);
},
__scrolltop:function(){ // 滚动条高度
var scrollTop=0;
if(document.documentElement&&document.documentElement.scrollTop){
scrollTop=document.documentElement.scrollTop;
}else if(document.body){
scrollTop=document.body.scrollTop;
}
return scrollTop;
},
url_split:function(url){ // 把 url 后的参数分割 返回 arr行
var url_arr = new Array();
if(! url){ url = window.location.href;}//没有传参表示用现在的url的
var urls_a = url.split('?');
if(! urls_a[1]) urls_a[1] = urls_a[0];
if(urls_a[1]){
urls_a2 = urls_a[1].split('&');
for(key in urls_a2){
urls_a3 = urls_a2[key].split('=');
if(urls_a3[0] && urls_a3[1]){
url_arr[urls_a3[0]] = urls_a3[1];
}
}
}
return url_arr;
},
url_com:function(url_arr){ // /* 把 url 的数组的数据 组合 */
var url = window.location.href;
var urls_a = url.split('?');
var aurl = urls_a[0];
aurl = aurl + '?';
var n = 0;
for(key in url_arr){
if(url_arr[key]){
if(n>0) aurl = aurl + '&';
// arr_key = decodeURIComponent(url_arr[key]); //encodeURI
arr_key = url_arr[key]+'';
arr_key = $1.__encodeURI(arr_key);
aurl = aurl + key + '=' + arr_key;
}
n++;
}
return aurl;
},
middle_site:function($screen , $min ){ // 计算中间位置
return ($screen/2)-($min/2);
},
obj_json:function(o){ // 对象转 json 字符串 0 必须是对象
var r = [];
if(typeof o =="string") return "\""+o.replace(/([\'\"\\])/g,"\\$1").replace(/(\n)/g,"\\n").replace(/(\r)/g,"\\r").replace(/(\t)/g,"\\t")+"\"";
if(typeof o =="undefined") return "\"\"";
if(typeof o == "object"){
if(o===null) return "null";
else if(!o.sort){
for(var i in o){ r.push("\""+i+"\""+":"+this.obj_json(o[i])); }
r="{"+r.join()+"}";
}else{
for(var i =0;i<o.length;i++) r.push(this.obj_json(o[i])) ;
r="["+r.join()+"]";
}
return r;
}else if(typeof o == "number"){
return "\""+o+"\"";
}else{
return "\"\"";
}
return o.toString();
},
__reload:function(){ //重新装入当前页面
window.location.reload();
},
__href:function(url){ // 页面跳转 与刷新网站
window.location.href = url;
},
__confirm:function(msg){ // 提示框
if(! msg){ msg = "确定?"; }
if (confirm(msg)==true){
return true;
}else{
return false;
}
},
time:function(){ // 获得 时间 秒 linux
var now = new Date();
return this.cint( now.getTime()/1000 );
},
is_int:function(str){ // 获得 整型 正负都是 return boolen
var re = new RegExp(/^(-|\+)?\d+$/);
return re.test(str);
},
is_positive_int:function(str){ // 正整 数 return boolen
var re = new RegExp(/^\d+$/);
return re.test(str);
},
is_float:function(str){ // 浮点型
var pos = new RegExp(/^(-|\+)?\d+(\.\d+)?$/);
return re.test(str);
},
is_not_string:function(str){ // 特殊字符 return boolen true 没有
var reg = /^[\da-zA-Z\-\_\*]*$/;
return reg.test(str);
},
is_phone:function(str){ // 手机格试
//var reg = new RegExp(/^[\d]{4}(\-)?(([\d]{7,8})|([\d]{3,4}))$/);
if(str.length!=11){
return false;
}
var reg = new RegExp(/^0{0,1}(13[0-9]|15[0-9]|18[0-9]|14[0-9]|16[0-9])[0-9]{8}$/);
return reg.test(str);
},
__focus:function(obj){ // 对象焦点的输入点移动到最后 只有 IE 如果是 query 这样传 a = $("#abc") $1.__focus(a[0])
if(isIE){ // js 这样传 $1.__focus($1.get_id("abc"))
try{
var r = obj.createTextRange();
r.moveStart('character',obj.value.length);
r.collapse(true);
r.select();
}catch(e){}
}
},
__encodeURI:function(url){ // url 转码
//if(url.indexOf("%")<0){
url = decodeURIComponent(url);
url = encodeURI(url);
//}
return url;
},
check_submit:function(id){ // 数据提交
document.getElementById(id).submit();
},
getCookie:function(c_name){ // 获得 cookie 的值
if(document.cookie.length>0){
c_start=document.cookie.indexOf(c_name + "=")
if(c_start!=-1){
c_start=c_start + c_name.length+1
c_end=document.cookie.indexOf(";",c_start)
if(c_end==-1) c_end=document.cookie.length;
return unescape(document.cookie.substring(c_start,c_end))
}
}
return ""
},
setCookie:function(name,value){ // 写cookies neme是key value 是值
var Days = 30;
var exp = new Date();
exp.setTime(exp.getTime() + Days*24*60*60*1000);
document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString();
},
delCookie:function(name){ //删除cookies
var exp = new Date();
exp.setTime(exp.getTime() - 1);
var cval = getCookie(name);
if(cval!=null){
document.cookie = name + "="+cval+";expires="+exp.toGMTString();
}
}
}
/*================== 常用对象 =====================*/
// 全局对象
public = {
deletegoods_cart_click_lock:0, // 锁
CART_HEAD_COUNT:5,
init:function(){
this.is_login();
try{
if(IS_STATIC==1){ // 是静态页
this.static_init(); // 静态页初使化
}
}catch(e){}
this.refurbish_click(); // 刷新跳转
this.deletegoods_cart(); // 购物车的删除
this.custom_server(); // 在线客服
},
is_login:function(){ // 判断有没有登陆
/*
var user = $1.getCookie('MEIZUSESSIONVAL');
var user_arr = $.parseJSON(user);
//if( !( user!='' && $1.cNumber(user_arr['uid'])>0) ){
var islogin = $1.getCookie('_islogin');
alert(document.cookie);
if( islogin){
alert('ok');
}
//}
*/
},
static_init:function(){ // 静态页初使化
var _self = this;
// 登陆状态
try{
var user = $1.getCookie('MEIZUSESSIONVAL');
var user_arr = $.parseJSON(user);
if(user!='' && $1.cNumber(user_arr['uid'])>0 ){
$("#mzLogout").attr( 'href' , SITE_URL+'/accounts/logout.htm');
$("#mzLogout").html('退出'); // mzLogout mzCustName
$("#mzCustName").attr('href','https://flyme.meizu.com/account/actmanage.jsp');
$("#mzCustName").attr('title','账户管理');
$("#mzCustName").html(user_arr['username']);
//$(".custHead").show();
$("#myorder_header").show();
$("#mzLoginArea1").show();
$("#mzLoginArea2").hide();
$("#mzLoginArea3").hide();
$("#mzLoginArea4").hide();
}else{
var _islogin = $1.getCookie("_islogin");
if(_islogin=='true'){
var thref = $("#mzLogin").attr('thref');
$1.__href(thref);
return ;
}
$("#mzLogout").attr( 'href' , SITE_URL+'/accounts/login.htm');
$("#mzLogout").html('登陆');
$("#mzCustName").attr('href','http://bbs.meizu.com/register.php');
$("#mzCustName").attr('title','');
$("#mzCustName").html('注册');
//$(".custHead").hide();
$("#myorder_header").hide();
}
}catch(e){}
// 购物车
var html = "";
var sum = 0;
try{
var cart_val_arr = {};
var extend=function(o,n,override){
for(var p in n)if(n.hasOwnProperty(p) && (!o.hasOwnProperty(p) || override))o[p]=n[p];
};
for(i=0;i<=5;i++){
var cart_val = $1.getCookie('mz_cart'+i); // 产品信息
if(cart_val!='null'){
var cart_val_arr2 = $.parseJSON(cart_val);
for(key in cart_val_arr2){
cart_val_arr[key] = cart_val_arr2[key];
}
}
}
//html = utf8to16( base64decode(cart_val_arr['html']) );
for(key in cart_val_arr){
sum++;
}
if(sum==0){ // 如果没有数据就去服务器取
_self.get_server_cart();
return ;
}else{
$n = 0;
for(key in cart_val_arr){
if(!($n<sum-_self.CART_HEAD_COUNT)){
str_html = "";
str_html += ' <li class="goodsitems_cartdiv">';
CartIMG = utf8to16( base64decode(cart_val_arr[key]['CartIMG']));
str_html += ' <div class="goodspic_cartdiv"><img src="'+CartIMG+'" /></div>';
str_html += ' <div class="goodsinfo_cartdiv">';
product_name = utf8to16( base64decode(cart_val_arr[key]['product_name']));
VerName = utf8to16( base64decode(cart_val_arr[key]['VerName']));
SN = utf8to16( base64decode(cart_val_arr[key]['SN']));
str_html += ' <h5><a href="'+SITE_URL+'/product/'+SN+'.htm'+'">'+product_name+' ('+VerName+')</a></h5>';
str_html += ' <p><span>¥'+cart_val_arr[key]['Price']+'</span>x<span>'+cart_val_arr[key]['number']+'</span></p>';
str_html += ' </div>';
str_html += ' <div class="deletegoods_cartdiv" ><span class="deletegoods_cart graybtn" href="javascript:void(0);" cart_id="'+key+'" >删除</span></div>';
str_html += '<div style="height:0px;overflow:hidden;clear:both;"></div>';
str_html += '</li>';
html = str_html+ html ;
}
$n++
}
}
}catch(e){}
_self.cart_assign( html , sum );
},get_server_cart:function(){
var _self = this;
if(document.cookie.length<=0){ // cookie 没有数据 才能去服务器去取
$.get('cart/get_static',{aa:'aa'} , function(data){
arr = $.parseJSON(data);
data_arr= arr['data'];
// 购物车的数据
_self.cart_assign(data_arr['cart']['html'] , data_arr['cart']['sum']);
});
}
},refurbish_click:function(){ // 按钮有跳转页面动作
$('.refurbish_click').live('click',function(){
var href = $1.trim($('.refurbish_click').attr('href'));
if(href){
$1.__href(href);
}
});
},
deletegoods_cart:function (){ // 删除购物车里的数据
var _self = this;
$(".deletegoods_cart").live('click',function(){
var self = this;
if(_self.deletegoods_cart_click_lock==1){
return ;
}
_self.deletegoods_cart_click_lock = 1;
var del = $(this).attr('cart_id');
$.get('cart/del?v='+$1.rand(1000000), {del:del} , function(data){
$arr = $.parseJSON(data);
if( $1.cint($arr['status'])==1 ){ // 成功
_self.cart_assign($arr['data']['html'] , $arr['data']['sum']);
}else{
}
_self.deletegoods_cart_click_lock = 0;
});
});
},
cart_assign:function($html,sum){ // 购物车赋值
$("#shopCartDiv").find("ul.head_cart").find('.goodsitems_cartdiv').remove();
if($html!=''){
$("ul.head_cart").find('.goodsitems_cartdiv_not').before($html);
}
$('.numincart_header').html(sum);
if($1.cint(sum)<=0){
$('.numincart_header').hide();
}else{
$('.numincart_header').show();
}
var cart_obj = $('li.goodsitems_cartdiv');
if(cart_obj.length<1){
$('li.goodsitems_cartdiv_not').show();
$('.tocheckout_cartdiv .refurbish_click').removeClass('bluebtn').addClass('wantage');
$('.tocheckout_cartdiv .refurbish_click').removeAttr('href');
}else{
$('li.goodsitems_cartdiv_not').hide();
$('.tocheckout_cartdiv .refurbish_click').removeClass('wantage').addClass('bluebtn');
$('.tocheckout_cartdiv .refurbish_click').attr('href', SITE_URL+'/cart');
}
},
tooltip_box_show:function(msg,left,top){ // 提示框 msg提示的信息 left位置 top 位置
this.tooltip_box_close(); // 关闭以前的框
var html= '';
html += '<div id="tip_violet_box" class="tip-violet" style="width:180px; visibility: inherit; left:'+left+'px; top:'+top+'px; border: 0px none; padding: 0px; background-image: none; background-color: transparent; opacity: 1;">';
html += ' <table cellspacing="0" cellpadding="0" border="0" >';
html += ' <tr>';
html += ' <td colspan="2" class="tip-top tip-bg-image" style="background-image: url(http://store.meizu.com/images/tip-violet.png);"><span></span></td>';
html += ' <td rowspan="2" class="tip-right tip-bg-image" style="background-image: url(http://store.meizu.com/images/tip-violet.png);"><span></span></td>';
html += ' </tr>';
html += ' <tr>';
html += ' <td rowspan="2" class="tip-left tip-bg-image" style="background-image: url(http://store.meizu.com/images/tip-violet.png);"><span></span></td>';
html += ' <td ><div class="tip-inner tip-bg-image" style="background-image: url(http://store.meizu.com/images/tip-violet.png);">'+msg+'</div></td>';
html += ' </tr>';
html += ' <tr>';
html += ' <td colspan="2" class="tip-bottom tip-bg-image" style="background-image: url(http://store.meizu.com/images/tip-violet.png);"><span></span></td>';
html += ' </tr>';
html += ' </table>';
html += ' <div class="tip-arrow tip-arrow-bottom" style="visibility: inherit;"></div>';
html += '</div>';
$('#storeContainer').append(html);
//$("#tip_violet_box").hide().show();// fadeIn(800)
},
tooltip_box_close:function(){ // 关闭提示框
$('#tip_violet_box').remove();
},
custom_server:function(){ // 在线客户
$("#custom_server").live('click',function(){
window.open('http://bbs.meizu.com/kf.php','_blank','height=473,width=703,fullscreen=3,top=200,left=200,status=yes,toolbar=no,menubar=no,resizable=no,scrollbars=no,location=no,titlebar=no,fullscreen=no');
});
$("#online_server").live('click',function(){
window.open('http://bbs.meizu.com/kf.php','_blank','height=473,width=703,fullscreen=3,top=200,left=200,status=yes,toolbar=no,menubar=no,resizable=no,scrollbars=no,location=no,titlebar=no,fullscreen=no');
});
}
}
/*********************** base64 加密与解码 ****************************/
var base64encodechars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
var base64decodechars = new Array(
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63,
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1,
-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1,
-1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1);
// 实例
/*
function doit() {
var f = document.f
f.output.value = base64encode(utf16to8(f.source.value))
f.decode.value = utf8to16(base64decode(f.output.value))
}
*/
function base64encode(str) {
var out, i, len;
var c1, c2, c3;
len = str.length;
i = 0;
out = "";
while (i < len) {
c1 = str.charCodeAt(i++) & 0xff;
if (i == len) {
out += base64encodechars.charAt(c1 >> 2);
out += base64encodechars.charAt((c1 & 0x3) << 4);
out += "==";
break;
}
c2 = str.charCodeAt(i++);
if (i == len) {
out += base64encodechars.charAt(c1 >> 2);
out += base64encodechars.charAt(((c1 & 0x3) << 4) | ((c2 & 0xf0) >> 4));
out += base64encodechars.charAt((c2 & 0xf) << 2);
out += "=";
break;
}
c3 = str.charCodeAt(i++);
out += base64encodechars.charAt(c1 >> 2);
out += base64encodechars.charAt(((c1 & 0x3) << 4) | ((c2 & 0xf0) >> 4));
out += base64encodechars.charAt(((c2 & 0xf) << 2) | ((c3 & 0xc0) >> 6));
out += base64encodechars.charAt(c3 & 0x3f);
}
return out;
}
function base64decode(str) {
var c1, c2, c3, c4;
var i, len, out;
len = str.length;
i = 0;
out = "";
while (i < len) {
do {
c1 = base64decodechars[str.charCodeAt(i++) & 0xff];
} while (i < len && c1 == -1);
if (c1 == -1)
break;
do {
c2 = base64decodechars[str.charCodeAt(i++) & 0xff];
} while (i < len && c2 == -1);
if (c2 == -1)
break;
out += String.fromCharCode((c1 << 2) | ((c2 & 0x30) >> 4));
do {
c3 = str.charCodeAt(i++) & 0xff;
if (c3 == 61)
return out;
c3 = base64decodechars[c3];
} while (i < len && c3 == -1);
if (c3 == -1)
break;
out += String.fromCharCode(((c2 & 0xf) << 4) | ((c3 & 0x3c) >> 2));
do {
c4 = str.charCodeAt(i++) & 0xff;
if (c4 == 61)
return out;
c4 = base64decodechars[c4];
} while (i < len && c4 == -1);
if (c4 == -1)
break;
out += String.fromCharCode(((c3 & 0x03) << 6) | c4);
}
return out;
}
function utf16to8(str) {
var out, i, len, c;
out = "";
len = str.length;
for (i = 0; i < len; i++) {
c = str.charCodeAt(i);
if ((c >= 0x0001) && (c <= 0x007f)) {
out += str.charAt(i);
} else if (c > 0x07ff) {
out += String.fromCharCode(0xe0 | ((c >> 12) & 0x0f));
out += String.fromCharCode(0x80 | ((c >> 6) & 0x3f));
out += String.fromCharCode(0x80 | ((c >> 0) & 0x3f));
} else {
out += String.fromCharCode(0xc0 | ((c >> 6) & 0x1f));
out += String.fromCharCode(0x80 | ((c >> 0) & 0x3f));
}
}
return out;
}
function utf8to16(str) {
var out, i, len, c;
var char2, char3;
out = "";
len = str.length;
i = 0;
while (i < len) {
c = str.charCodeAt(i++);
switch (c >> 4) {
case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7:
// 0xxxxxxx
out += str.charAt(i - 1);
break;
case 12: case 13:
// 110x xxxx 10xx xxxx
char2 = str.charCodeAt(i++);
out += String.fromCharCode(((c & 0x1f) << 6) | (char2 & 0x3f));
break;
case 14:
// 1110 xxxx 10xx xxxx 10xx xxxx
char2 = str.charCodeAt(i++);
char3 = str.charCodeAt(i++);
out += String.fromCharCode(((c & 0x0f) << 12) |
((char2 & 0x3f) << 6) |
((char3 & 0x3f) << 0));
break;
}
}
return out;
}
/*********************** base64 加密与解码 end ****************************/
/**
* 产口列表页 分页跳转
*
**/
function goodslist_click_page($page,$url){
postfix = "";
/*
try{
if(IS_STATIC==1){
postfix = '.html';
}else{
postfix = '.htm';
}
}catch(e){
postfix = '.htm';
}
*/
postfix = '.htm';
$url = $url + '_' + $page + postfix;
$1.__href($url);
}
/**
*
*/
function myorder_click_page($page,$url){
postfix = '.htm';
$url = $url + postfix + '?page=' + $page;
$1.__href($url);
}
$(function(){
public.init(); // 全局初使化
});
Javascript------魅族官网公用 js 收集
最新推荐文章于 2022-06-22 16:49:41 发布