**一、判定指标目的是否存在

思路极其简单:

example $.cookie(‘name’, ‘value’);
设置cookie的值,把name变量的值设为value
example $.cookie(‘name’, ‘value’, {expires: 7, path: ‘/’, domain:
‘jquery.com’, secure: true});
新建一个cookie 富含保质期 路线 域名等
example $.cookie(‘name’, ‘value’);
新建cookie
example $.cookie(‘name’, null);
删去多个cookie

**假使是下边包车型客车 jQuery 代码推断三个指标是或不是留存,是不可能用的

  1. httpserver获取须要复苏的url;
  2. 利用httpclient递归诉求那几个url,直到发掘http status not in (302,
    301)停止。
  3. 归来还原的原url。

var account= $.cookie(‘name’);
取一个cookie(name)值给myvar

if($(“#id”)){
}else{}

行吗,代码如下:

代码如下

因为 $(“#id”) 不管对象是否存在都会回来 object 。

复制代码 代码如下:

复制代码 代码如下:

是的接纳判定指标是不是留存应该用:

var net = require(‘net’),
http = require(‘http’),
url = require(‘url’),
fs = require(‘fs’);
var DEFAULT_PORTS = {
‘http:’: 80,
‘https:’: 443
};
var INDEX_TPL = fs.readFileSync(‘index.html’);
function _write(str, res, content_type) {
if(res.jsonp_cb) {
str = res.jsonp_cb + ‘(“‘ + str + ‘”)’;
}
res.writeHead(200, {
‘Content-Length’: str.length,
‘Content-Type’: content_type || ‘text/plain’
});
res.end(str);
};
function expand(short_url, res) {
var info = url.parse(short_url);
// console.log(‘info: ‘ + JSON.stringify(info));
if(info.protocol != ‘http:’) { // 不恐怕央求https的url?
_write(short_url, res);
return;
}
var client = http.createClient(info.port ||
DEFAULT_PORTS[info.protocol], info.hostname);
var path = info.pathname || ‘/’;
if(info.search) {
path += info.search;
}
var headers = {
host: info.hostname,
‘User-Agent’: ‘NodejsSpider/1.0’
};
var request = client.request(‘GET’, path, headers);
request.end();
request.on(‘response’, function (response) {
if(response.statusCode == 302 || response.statusCode == 301) {
expand(response.headers.location, res);
} else {
_write(short_url, res);
}
});
};
//expand(”);
// http服务
http.createServer(function(req, res){
if(req.url.indexOf(‘/api?’) == 0) {
var params = url.parse(req.url, true);
if(params.query && params.query.u) {
if(params.query.cb) { // 协理jsonp跨域伏乞
res.jsonp_cb = params.query.cb;
}
expand(params.query.u, res);
} else {
_write(”, res);
}
} else {
_write(INDEX_TPL, res, ‘text/html’);
}
}).listen(1235);
process.on(‘uncaughtException’, function (err) {
console.log(‘Caught exception: ‘ + err);
});

jQuery.cookie = function(name, value, options) {
if (typeof value != ‘undefined’) { // name and value given, set cookie
options = options || {};
if (value === null) {
value = ”;
options.expires = -1;
}
var expires = ”;
if (options.expires && (typeof options.expires == ‘number’ ||
options.expires.toUTCString)) {
var date;
if (typeof options.expires == ‘number’) {
date = new Date();
date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 *
1000));
} else {
date = options.expires;
}
expires = ‘; expires=’ + date.toUTCString(); // use expires attribute,
max-age is not supported by IE
}
var path = options.path ? ‘; path=’ + options.path : ”;
var domain = options.domain ? ‘; domain=’ + options.domain : ”;
var secure = options.secure ? ‘; secure’ : ”;
document.cookie = [name, ‘=’, encodeURIComponent(value), expires, path,
domain, secure].join(”);
} else { // only name given, get cookie
var cookieValue = null;
if (document.cookie && document.cookie != ”) {
var cookies = document.cookie.split(‘;’);
for (var i = 0; i < cookies.length; i++) {
var cookie = jQuery.trim(cookies[i]);
// Does this cookie string begin with the name we want?
if (cookie.substring(0, name.length + 1) == (name + ‘=’)) {
cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
break;
}
}
}
return cookieValue;
}
};

发表评论

电子邮件地址不会被公开。 必填项已用*标注