[英]Trying to write cookie if browser is IE8 or Mobile Safari, and localstorage if any other browser - using Javascript
So, I have a variable I am creating that is passed along with POST data from a form. 因此,我有一个正在创建的变量,该变量与来自表单的POST数据一起传递。 Due to some sort of code quirk, any modern browser will create the variable twice, and trigger two different unique id's for the same event, if setting the value of the variable to a cookie.
由于某种形式的代码怪癖,如果将变量的值设置为cookie,则任何现代浏览器都会两次创建该变量,并为同一事件触发两个不同的唯一ID。 If a modern browser sets the value to localstorage instead, only one unique id is generated and everything works fine.
如果现代浏览器改为将值设置为localstorage,则仅生成一个唯一的ID,并且一切正常。
However, Mobile Safari doesn't treat localstorage as it should be(because Apple moved the directory for localstorage to one that gets emptied randomly), and IE8 fails to set the value to localstorage. 但是,Mobile Safari不会像对待本地存储那样对待本地存储(因为Apple将本地存储目录移到了随机清空的目录中),并且IE8无法将值设置为本地存储。 In order to bypass these issues, those two browsers should set a cookie instead, which works as intended and generates a single unique identifier for the event.
为了绕过这些问题,这两个浏览器应改为设置一个cookie,该cookie可以按预期工作并为该事件生成一个唯一的标识符。
I cannot just use feature detection, because IE8 technically supports localstorage, but not when the document type of the page is anything other than HTML5. 我不能仅使用功能检测,因为IE8从技术上支持本地存储,但是当页面的文档类型不是HTML5时,则不支持。 Mobile Safari also supports localstorage, but treats it differently than other modern browsers.
Mobile Safari也支持本地存储,但与其他现代浏览器不同。
I can tell that the two checks are working to identify Chrome as "anything else", however the unique identifier is showing up as undefined. 我可以说这两项检查可以将Chrome识别为“其他”,但是唯一标识符显示为未定义。
What is going wrong with the code below that is preventing the variable from being set and stored in localstorage? 下面的代码怎么了才能阻止设置变量并将其存储在localstorage中?
$(document).ready(function() {
if ($.browser.msie && parseInt($.browser.version, 10) === 8) {
var uuid = $.cookie("uuid");
if(typeof uuid === 'undefined'){
var uuid = guid();
document.cookie='uuid='+uuid;
var iam = "ie8";
alert(iam);
}
} else {
if (navigator.userAgent.match(/(iPad|iPhone|iPod touch);.*CPU.*OS 7_\d/i)){
var uuid = $.cookie("uuid");
if(typeof uuid === 'undefined'){
var uuid = guid();
document.cookie='uuid='+uuid;
var iam = "safari mobile";
alert(iam);
} else {
if (localStorage.getItem("uuid") === null) {
var uuid = guid();
localStorage.setItem("uuid", uuid);
var iam = "anything else";
alert(iam);
}
}
}
}
});
Things to note: 注意事项:
You have an else { if ()
where you likely meant to have else if (){
您有一个
else { if ()
,您可能打算拥有else if (){
http://jsfiddle.net/FE2AQ/1/ http://jsfiddle.net/FE2AQ/1/
//$(document).ready(function () { // this isn't really needed
if ($.browser.msie && parseInt($.browser.version, 10) === 8) {
var uuid = $.cookie("uuid");
if (typeof uuid === 'undefined') {
var uuid = guid();
document.cookie = 'uuid=' + uuid;
var iam = "ie8";
alert(iam);
}
} else if (navigator.userAgent.match(/(iPad|iPhone|iPod touch);.*CPU.*OS 7_\d/i)) {
var uuid = $.cookie("uuid");
if (typeof uuid === 'undefined') {
var uuid = guid();
document.cookie = 'uuid=' + uuid;
var iam = "safari mobile";
alert(iam);
}
} else {
if (localStorage.getItem("uuid") === null) {
var uuid = guid();
localStorage.setItem("uuid", uuid);
var iam = "anything else";
alert(iam);
}
}
//});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.