dream0776 2014-06-18 21:37
浏览 18
已采纳

Javascript依赖关系不通过Ajax加载

I have an index.php file that will load content based on a $_GET variable. So, it basically functions like this...

<?php
   $problem_id = $_GET['problem_id'];
   include('include/' . $problem_id . '.php');
?>

So, if a user clicks problem type #8, 8.php will be included in the index.php file and it's content will be displayed.

The problem is that I'm now using AJAX to load 8.php (or whatever file is selected). Some of the files depend on other javascript files (i.e. <script type="text/javascript" src="/include/js/api/utils.js"></script>) to work. It seems that these dependencies are not being loaded when I run the index.php file via AJAX. If I run it with a normal $_GET variable in the URL (i.e. http://myscript.com?problem_id=8), it works fine.

Is this a known issue with AJAX? Anyway around it?

Thanks.

  • 写回答

1条回答 默认 最新

  • dongyimo1293 2014-06-18 21:46
    关注

    You have not specified where the required javascript files are being loaded. However, if we assume they are being included by your php script, then yes they will work when you access that php script directly but when you include it via ajax they will not.

    My suggestion which is the simple approach, is that in your calling page (index.php) you include all your javascript so that it is ready to go, regardless of which content is dynamically loaded in.

    So load the dependencies always in advance, and don't include them in your included php files.

    When you load content via ajax it really needs to be html which can be incorporated into the existing page DOM, it's not so straight forward to be loading javascript and executing it although that is possible (using requirejs or similar) but I think probably the above simple approach will work for you.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 我的R语言提示去除连锁不平衡时clump_data报错,图片以下所示,卡了好几天了,苦恼不知道如何解决,有人帮我看看怎么解决吗?
  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序