避免在raphael.sketchpad函数中使用eval

时间:2014-06-27 10:04:49

标签: javascript jquery json raphael eval

我有以下代码按预期工作:

                var svg_height = $("#svg-container").height();
                var svg_width = $("#svg-container").width();
                var name = "sketchpad";

    eval("window." + name + "= Raphael.sketchpad('"
                                + "svg-container" + "',{" + " width:"
                                + svg_width + "," + "height: " +           svg_height
                                + "," + "editing: true," + " });");

但是出于安全考虑,我想避免使用eval。 我尝试了以下但是它不起作用

window.sketchpad = Raphael.sketchpad("'svg-container',{width:"+ svg_width +",height:"+ svg_height +", editing: true}");

可以使用JSON解决这个问题。如果有,怎么样? 任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:2)

免责声明:我对Raphael一无所知,只是翻译您的eval代码。

window[name] = Raphael.sketchpad("svg-container", {
                                        width: svg_width,
                                        height: svg_height,
                                        editing: true
                 });

你所拥有的功能几乎就在那里,只是有一些错误的引用弄乱了它。

答案 1 :(得分:2)

这也应该有用。

window.sketchpad = Raphael.sketchpad("svg-container", {
                                        width: svg_width,
                                        height: svg_height,
                                        editing: true
                 });

此处无需使用名称变量。

相关问题