避免在raphael中使用eval.画板功能

Avoiding use of eval in raphael.sketchpad function

本文关键字:eval 功能 raphael      更新时间:2023-09-26

我有下面的代码,按预期工作:

                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解决这个问题吗?如果是,怎么做?

免责声明:我对拉斐尔一无所知,只是翻译你的eval代码。

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

你的函数已经差不多了,只是有几个错误的引号把它弄乱了。

这个应该也可以。

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

这里不需要使用name变量