我有一个air应用程序,其中用户在文本区域中输入javascript,并在mx:HTML组件中对其进行评估,但即使在评估周围以及评估中的代码周围使用try/catch,和 HTMLUncaughtScriptExceptionEvent 处理程序,它仍然会抛出错误。 htmlWindow 是 html.htmlLoader.window。
try { htmlWindow.eval("try {" + script.text + "} catch (error:Error) { trace(error) }); } catch (error:Error) { trace(error) }
当我在script
中输入文本时,该行上的应用程序错误,与
ReferenceError: Can't find variable: d
at Main/reloadHTML()[C:\Users\Christian\Adobe Flash Builder 4.5\JavaScript plus Scratch\src\Main.mxml:264]
at Main/__script_change()[C:\Users\Christian\Adobe Flash Builder 4.5\JavaScript plus Scratch\src\Main.mxml:324]
at flash.events::EventDispatcher/dispatchEventFunction()
at flash.events::EventDispatcher/dispatchEvent()
at mx.core::UIComponent/dispatchEvent()[E:\dev\4.5.1\frameworks\projects\framework\src\mx\core\UIComponent.as:13128]
at spark.components.supportClasses::SkinnableTextBase/textDisplay_changeHandler()[E:\dev\4.5.1\frameworks\projects\spark\src\spark\components\supportClasses\SkinnableTextBase.as:2265]
at flash.events::EventDispatcher/dispatchEventFunction()
at flash.events::EventDispatcher/dispatchEvent()
at mx.core::UIComponent/dispatchEvent()[E:\dev\4.5.1\frameworks\projects\framework\src\mx\core\UIComponent.as:13128]
at spark.components::RichEditableText/textContainerManager_flowOperationCompleteHandler()[E:\dev\4.5.1\frameworks\projects\spark\src\spark\components\RichEditableText.as:4808]
at flash.events::EventDispatcher/dispatchEventFunction()
at flash.events::EventDispatcher/dispatchEvent()
at flashx.textLayout.container::TextContainerManager/dispatchEvent()[C:\Vellum\branches\v2\2.0\dev\output\openSource\textLayout\src\flashx\textLayout\container\TextContainerManager.as:1553]
at flash.events::EventDispatcher/dispatchEventFunction()
at flash.events::EventDispatcher/dispatchEvent()
at flashx.textLayout.elements::TextFlow/dispatchEvent()[C:\Vellum\branches\v2\2.0\dev\output\openSource\textLayout\src\flashx\textLayout\elements\TextFlow.as:859]
at flashx.textLayout.edit::EditManager/finalizeDo()[C:\Vellum\branches\v2\2.0\dev\output\openSource\textLayout\src\flashx\textLayout\edit\EditManager.as:669]
at flashx.textLayout.edit::EditManager/doOperation()[C:\Vellum\branches\v2\2.0\dev\output\openSource\textLayout\src\flashx\textLayout\edit\EditManager.as:613]
at flashx.textLayout.edit::EditManager/flushPendingOperations()[C:\Vellum\branches\v2\2.0\dev\output\openSource\textLayout\src\flashx\textLayout\edit\EditManager.as:873]
at flashx.textLayout.edit::SelectionManager/enterFrameHandler()[C:\Vellum\branches\v2\2.0\dev\output\openSource\textLayout\src\flashx\textLayout\edit\SelectionManager.as:1859]`
有什么办法可以阻止这个错误吗?我试过script.change="html.htmlText = '<script>' + script.text + '</script'>"
,但我有htmlWindow.log = log; htmlWindow.rotateSprite = rotateSprite;
,如果我复制粘贴 log('test')
在更改它之前,它可以工作,如果我之前或之后更改它,它就永远不会再工作,所以我希望让这个评估工作。
最佳答案
我不确定您能否让这段代码 100% 没有异常。用户可以编写完全无效的代码,例如 zlçrza ;à"çé°$
,它不会抛出异常,而只是使 JS 编译器崩溃。您能解释一下为什么让用户直接输入 javascript 代码吗?也许您需要以其他方式解决这个问题?
关于javascript - 使用 try/catch 评估抛出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8306437/