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 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测