简体   繁体   English

在IE7 / IE8中获取尾随逗号(在JavaScript中)

[英]Get Trailing commas( in JavaScript ) working in IE7/IE8

You find much infos to trailing commas here: Are trailing commas in arrays and objects part of the spec? 您可以在此处找到有关尾随逗号的大量信息: 数组和对象中的尾随逗号是否属于规范的一部分?

It's about the comma at "right there". 关于“在那儿”的逗号。

[
        { title: "..", display: function() { return ".."; } },
        { title: "..", display: function() { return ".."; } },
        { title: "..", display: function() { return ".."; } }, // <--right there
    ]

My problem is now, that a 3rd party tool generates this list with a trailing comma and I am not able to access the sourcecode of this tool. 我的问题是,第3方工具生成的列表后面带有逗号,而我无法访问此工具的源代码。 I 一世

<body>
my stuff
<!-- the folloging div with the javascript comes from the 3rd party -->
<div class="item"><script type="text/javascript">
$(document).ready(function() {


    $.supersized({

        // Functionality
        slide_interval          :   8000,       // Length between transitions
        transition              :   1,          // 0-None, 1-Fade, 2-Slide Top, 3-Slide Right, 4-Slide Bottom, 5-Slide Left, 6-Carousel Right, 7-Carousel Left
        transition_speed        :   400,        // Speed of transition
        fit_always              :   0,          //Prevents the image from being cropped by locking it at 100% width.
        min_width               :   600,        //min-width of images
        random                  :   0,      //random img
        vertical_center         :   1,          //vertical alignment

        // Components                           
        slide_links             :   'false',    // Individual links for each slide (Options: false, 'number', 'name', 'blank')
        slides                  :   [           // Slideshow Images

    {image : 'image1.jpg', otherstuff: 'blah', moreotherstuff: 'lorem'},

    {image : 'image2.jpg', otherstuff: 'blub', moreotherstuff: 'ipsum'},


                                    ]
    });
});
</script></div>

And now I am asking me is there any way to get this working in IE7/IE8. 现在,我问我有什么方法可以使它在IE7 / IE8中工作。

The only way I see right now (as i can't edit the 3rd party and can't access it on the server) is: - give a special output without that div - request the normal side via ajax - parse everything, get this div, delete last comma and execute the javascript 我现在看到的唯一方法(因为我无法编辑第三方并且无法在服务器上访问它)是:-提供没有该div的特殊输出-通过ajax请求正常显示-解析所有内容,获取此信息div,删除最后一个逗号并执行javascript

that's realy not the way I want do it. 那真的不是我想要的方式。 Is there another way I could solve this? 有没有其他方法可以解决这个问题?

IE7/IE8 will report 1 element bigger array length, unless you trim those arrays. IE7 / IE8会报告数组长度增加1个元素,除非您修剪这些数组。

If you know all JS methods which accepts data from 3rd party you can write wrapper for them. 如果您知道所有接受第三者数据的JS方法,则可以为其编写包装器。 For example: 例如:

var removeEmpty = function(array){
    var i, result = [];

    for (i = 0; i < array.length; ++i){
        if ( typeof array[i] != 'undefined' ){
            result.push(array[i]);
        }
    }

    return result;
};

$.supersized = (function() {
    var origMethod = $.supersized;

    return function(config) {
        config.slides = removeEmpty(config.slides); // get rid of undefined data in array
        return origMethod.call(this, config); // call original method
    };
}());

No, there is no way to get those trailing commas working in IE. 不,没有办法让那些尾随的逗号在IE中工作。 It simply can't be done; 根本无法做到; it's a fundamental part of the browser's javascript parser. 这是浏览器javascript解析器的基本组成部分。

This is an extremely well-known issue, which means that your third party tool is very obviously faulty and clearly hasn't been tested properly. 这是一个非常著名的问题,这意味着您的第三方工具很明显是错误的,并且显然没有经过适当的测试。 I don't know where you got it from, but if you paid for it, I'd be complaining loudly if I were you. 我不知道你从哪里得到的,但是如果你付了钱,我会大声抱怨如果我是你。

If you really can't deal with the problem at source, then your only option remaining is to fix the generated code before it gets into IE. 如果您真的不能从源头上解决问题,那么剩下的唯一选择就是在生成的代码进入IE之前对其进行修复。 That much is easier said than done. 说起来容易做起来难。 Depending on the complexity of the generated code, you might be able to write a regex for it if it's simple and predictable, but if the code it generates is complex or of varying complexity, I suspect you may be in trouble with that idea. 根据所生成代码的复杂性,如果它简单且可预测,则可能可以为其编写正则表达式,但是如果它生成的代码复杂或复杂程度不同,我怀疑您可能会遇到这种想法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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