简体   繁体   English

如果浏览器是IE8或Mobile Safari,则尝试编写Cookie;如果使用其他浏览器,则尝试编写本地存储-使用Javascript

[英]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: 注意事项:

  • There is a function that is called in 3 second intervals that posts this data over to a PHP processing file using Ajax. 每隔3秒就会调用一个函数,该函数使用Ajax将这些数据发布到PHP处理文件中。
  • There is a function called guid that generates a 20 character unique code. 有一个称为guid的函数,可生成20个字符的唯一代码。
  • Browsers we specifically need to check for: IE8(and possibly below) and Mobile Safari(ie, on iPad, iPhone, or iPod). 我们特别需要检查的浏览器:IE8(可能更低)和Mobile Safari(例如,在iPad,iPhone或iPod上)。
  • In Google Chrome and Firefox, uuid is undefined with the current script structure, when instead it should be locally stored and feature a string generated by the guid function. 在Google Chrome和Firefox中,uuid在当前脚本结构中未定义,而应在本地存储,并使用guid函数生成的字符串。
  • Above this code are links to jQuery 1.8.2(to gain access to $.browser) and jQuery.cookie.js 此代码上方是jQuery 1.8.2(以获得对$ .browser的访问)和jQuery.cookie.js的链接。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM