【问题标题】:how to use Jquery getScript caching using dynamically generated url如何使用动态生成的 url 使用 Jquery getScript 缓存
【发布时间】:2018-08-11 21:06:33
【问题描述】:

我正在寻找一种使用动态生成的 url 缓存脚本的方式我目前正在使用 Octobercms,它将所有脚本组合到一个渲染文件中我在过去看到人们使用 php 但十月 cms 使用刀片我看过的脚本是这个

$.cachedScript( "ajax/test.js" ).done(function( script, textStatus ) {
  console.log( textStatus );
});

我遇到的问题是组合脚本的 url 在安装时是不同的。

【问题讨论】:

  • 我不确定 jquery 但您是否尝试过类似 " $.cachedScript("{{ ['ajax/test.js']|theme }}").done(function(script, textStatus){ console.log(textStatus); });"
  • 我已经得到了这个工作 $.getScript('yoursite.com/test/combine/…', function() { // 调用脚本中定义的自定义函数,但仍然得到一个错误“速度已加载” });仍然收到一个错误“速度已加载”,但尚未找到动态生成 url 的方法
  • 获取动态脚本 url 试试这个 var path= $("script[src]:eq(1)").attr('src');由于 jquery 是零基 eq(1) 实际上是第二个脚本文件,只需删除您的 url 和引号并替换为路径
  • 不幸的是 user2033464 这不起作用如果我硬编码问题中的 url 我只能让它工作

标签: javascript jquery octobercms


【解决方案1】:

我已经弄清楚如何使用以下方法来解决“速度已加载”错误的问题

$("#loader").load(href, function(){
        delete jQuery.Velocity;
         $.getScript( "http://yoursite.com/test/combine/b075f6a5b111b3375ecc553c0d813ee5-1534701438", function() { 
     // Call custom function defined 
     });

只需要弄清楚如何替换“http://yoursite.com/test/combine/b075f6a5b111b3375ecc553c0d813ee5-1534701438”,因为这会改变这部分代码的任何帮助我已经尝试过 user2033464 代码但是当我 console.log(path);在 $.getScript 之后我得到“ReferenceError: path is not defined”

【讨论】:

    【解决方案2】:

    我已经解决了我的问题如下

    var path= $("script[src]:eq(1)").attr('src'); //needs to be globle to work in getScript
    $(document).ready(function() {
        $("#loader").load(href, function(){
            delete jQuery.Velocity;  // stops Velocity reloading error
             $.getScript( path, function() { // loads script into newly loaded page into existing page 
         // Call custom function defined 
         });
    });
    

    希望对有类似问题的人有所帮助,感谢 user2033464 的帮助

    【讨论】:

      猜你喜欢
      • 2012-10-04
      • 1970-01-01
      • 2023-03-28
      • 1970-01-01
      • 1970-01-01
      • 2014-06-11
      • 2010-12-27
      • 1970-01-01
      • 2011-04-09
      相关资源
      最近更新 更多