如何在运行时修改javascript代码?
有没有办法在调试时修改 JavaScript 代码? Visual Studio具有“编辑并继续”功能,在Java和其他语言中也可以完成类似的代码热交换。这可以用 JavaScript 来完成吗?如果可以,怎么做?
Is there a way to modify JavaScript code while debugging? Visual Studio has "Edit and Continue", and similar hot swapping of code can be done in Java and other languages. Can this be done with JavaScript, and if so, how?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Chrome、Safari 和其他一些基于 WebKit 的浏览器在 Web 检查器中包含一项称为“实时编辑”的功能。如果您转到“脚本”面板并在断点处停止(或者即使没有在断点处停止 - 我不确定),您可以双击一行并开始编辑该行。您所做的更改将对脚本生效。
Chrome, Safari, and some other WebKit-based browsers contain a feature in the Web Inspector known as Live Edit. If you go to the Scripts panel and are stopped on a breakpoint (or maybe even if not stopped on a breakpoint — I'm not sure), you can double click on a line and start editing that line. The changes you make will take effect on the script.
有了 Chrome 开发者工具,这变得非常简单。
只需弹出打开检查器,单击“脚本”选项卡,从下拉菜单中选择您想要的脚本,然后您就可以自由编辑脚本并添加断点。如果刷新页面,断点将保留在那里。
如果您看过 Paul Irish 的演讲,他会向您展示如何动态编辑脚本
http://paulirish.com/2011/a-re-introduction-to-the-chrome-developer-tools/
也不错:
http://blip.tv/jsconf/jsconf2011-paul-irish-5382827
With Chrome Developer tools, this is super easy.
Just pop open inspector, click on the
scripts
tab, select which one you want from the dropdown menu and then you are free to edit the script and add in breakpoints. If you refresh the page, your breakpoints will stay there.If you watch this talk but Paul Irish, he shows how you can edit a script on the fly
http://paulirish.com/2011/a-re-introduction-to-the-chrome-developer-tools/
also good:
http://blip.tv/jsconf/jsconf2011-paul-irish-5382827
如果你说的是调试时,修改正在运行的代码是非常容易的。在调试控制台中,您可以输入 Javascript 表达式,它将在窗口上下文中运行,该窗口包含代码的所有对象和函数,因此您可以通过重新定义它们来交换它们。
If you're talking about while debugging, it's very easy to modify the running code. In your debugging console, you can enter in Javascript expressions and it will run in the context of the window, which contains all the objects and functions of your code, so you can swap them out by redefining them.
因为 JavaScript 可以修改 DOM,所以您实际上必须更改 JavaScript 文件,保存并重新加载。对我来说,我喜欢 IE,所以我在浏览器中运行网页,而不是在调试模式下。然后您可以更改脚本文件,保存它们。切换回浏览器并重新加载 (F5) 以查看更改。据说 IE 11 具有这种能力(可能像 Chrome 一样,这本质上就是我正在做的事情,从我所知道的改变保存重新加载),但我无法找到它,更不用说打开 js 文件了。超级糟糕的用户界面。我猜测打开文件图标的浏览器是 但它始终呈灰色。
Because JavaScript can modify the DOM the you essentially have to change the JavaScript file, save it and reload. For me, I like IE so I run the webpage in a browser NOT IN DEBUG mode. Then you can change the script files, SAVE them. switch back to the browser and reload (F5) to see your changes. Supposedly IE 11 has this ability (probably like Chrome which is essentially what I am doing, changing saving reloading from what I can tell) but I can't get it to find, let alone open a js file. Super poor UI. I'm guessing the browser to open the file icon is but it is always grayed out.