weixin_33694172 2011-08-09 03:34 采纳率: 0%
浏览 20

动态Javascript导入

I'm making a web page that needs to be able to dynamically change the JavaScript code that's running. Many times, functions in the dynamic code have the same names, but behave differently, so using document.getElementsByTagName('head')[0].appendChild(script); isn't an option. Right now, I've got <script type="text/javascript" id="dynamicScript" src="about:blank"></script> in the head of the document, and when I want to change the active script, I call document.getElementById('dynamicScript').src="ajax.php?get=script&script=" + page;, and that PHP page spits out the corrent script (without tags). It doesn't work, though. Am I doing something wrong, or is there something else I could to that would also work?

  • 写回答

2条回答 默认 最新

  • weixin_33674976 2011-08-09 03:42
    关注

    You need to add a new <script> element to the DOM, not just change it. Also there's no way for you to unload your old script without reloading the page. If you need to remove functions and data that were declared by a previous script, you need to do it programmatically. Replacing functions works.

    Finally I recommend using a script loader like LABjs, RequireJS, ControlJS, or SexyJS. Doing it manually is extra work.

    Edit: You could also just use getScript() if you're using jQuery and don't need the extra control over what order your scripts are loaded in.

    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料