Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/475.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/cmake/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 处理prototype和jQuery,在同一页面中包含外部js文件_Javascript_Jquery_Html_Prototypejs - Fatal编程技术网

Javascript 处理prototype和jQuery,在同一页面中包含外部js文件

Javascript 处理prototype和jQuery,在同一页面中包含外部js文件,javascript,jquery,html,prototypejs,Javascript,Jquery,Html,Prototypejs,我正在处理一个集成问题,处理一个旧网站,它有许多基于原型的脚本。我必须将它集成到一个外部网站的模板中,该模板使用一个脚本(我们称之为externaljqueryscript.js),我无法控制它,它使用jquery externaljqueryscript.js使用了$操作符,显然脚本与原型冲突。问题特别在于externaljqueryscript.js(我们称之为externalFunction())中的一个函数,该函数是模板正常工作所必需的 我无法更改外部模板,但我可以只填写可用的占位符 在

我正在处理一个集成问题,处理一个旧网站,它有许多基于原型的脚本。我必须将它集成到一个外部网站的模板中,该模板使用一个脚本(我们称之为
externaljqueryscript.js
),我无法控制它,它使用jquery

externaljqueryscript.js
使用了
$
操作符,显然脚本与原型冲突。问题特别在于
externaljqueryscript.js
(我们称之为
externalFunction()
)中的一个函数,该函数是模板正常工作所必需的

我无法更改外部模板,但我可以只填写可用的占位符

在集成产生的页面中,我有以下内容:

<html>
  <!-- external template's stuff - begin -->
...
  <script src="http://www.external-website.com/js/common/jquery-1.8.0.min.js"></script>
  <script type="text/javascript" src="http://www.external-website.com/js/common/external-jquery-script.js"></script>
...
  <!-- external template's stuff - end -->
  <!-- my page's stuff - begin -->
...
  <script type="text/javascript" src="http://www.my-website.com/js/prototype.min.js"></script>
  <script type="text/javascript" src="http://www.my-website.com/js/script.js"></script>
...
  <!-- my page's stuff - end -->
... <!-- other contents... --> ...
</html>
我有两个问题:

  • 有没有办法包含
    外部jqueryscript.js
    告诉脚本使用
    $
    操作符作为jquery操作符
  • 如果第一个问题的答案是“否”,你会建议我如何解决这个问题
    我找到了解决问题的方法,我将在这里与大家分享

    因为除了“我的页面的内容”区域外,我无法更改模板,所以我决定覆盖模板的jQuery部分,通过cron自动下载脚本,并在原始版本之后用原型/冲突安全版本(包括页面中的固定脚本)自动替换jQuery调用

    现在,由于脚本添加了一个监听器
    $(document).ready
    ,我还必须用一个原型版本替换它,在某种意义上禁用jQuery监听器

    下面是执行自动替换的脚本(
    $1
    表示js脚本名称,作为参数传递):

    在包含此新脚本之前,我还添加了以下脚本,以避免冲突并防止触发
    $(document)。准备好

    <script>
    <!--
            jQuery = jQuery.noConflict(true); //After this jQuery is callable only throught the jQuery variable
            jQuery.holdReady(true); //After this jQuery(document).ready stays pending, waiting for a jQuery.holdReady(false) that will never come, turning off the listener in the first jQuery script, that would cause problems otherwise
    -->
    </script>
    

    只需定义要用于js库的变量名。问题的可能重复之处在于,我无法编辑代码,以便对不同于$()的函数进行jQuery或Prototype调用然后你会陷入困境,因为它们不能同时使用相同的东西,这就是为什么我问是否有任何方法可以通过html include语句来指定$external-jQuery-script.js中关于特定脚本的含义(在这种特定情况下,我更希望$REFEREJQUERY只用于external-jQuery-script.js,其他地方都有原型)
    #!/bin/bash
    sed -i "s#\$(document)\.ready(#document\.observe(\'dom\:loaded\'\,#g" $1 ##THIS REPLACES $(document).ready with the prototype's equivalent
    sed -i 's#\$(#jQuery(#g' $1 ##THIS REPLACES $( with jQuery(
    sed -i 's#\$\.#jQuery\.#g' $1 ##THIS REPLACES $. with jQuery.
    sed -i 's#(\$)#(jQuery)#g' $1 ##THIS REPLACES ($) with (jQuery)
    
    <script>
    <!--
            jQuery = jQuery.noConflict(true); //After this jQuery is callable only throught the jQuery variable
            jQuery.holdReady(true); //After this jQuery(document).ready stays pending, waiting for a jQuery.holdReady(false) that will never come, turning off the listener in the first jQuery script, that would cause problems otherwise
    -->
    </script>
    
    <html>
      <!-- external template's stuff - begin -->
    ...
      <script src="http://www.external-website.com/js/common/jquery-1.8.0.min.js"></script>
      <script type="text/javascript" src="http://www.external-website.com/js/common/external-jquery-script.js"></script>
    ...
      <!-- external template's stuff - end -->
      <!-- my page's stuff - begin -->
    ...
      <script>
      <!--
            jQuery = jQuery.noConflict(true);
            jQuery.holdReady(true);
      -->
      </script>
      <script type="text/javascript" src="http://www.my-website.com/js/prototype.min.js"></script>
      <script type="text/javascript" src="http://www.my-website.com/js/external-jquery-script-fixed.js"></script>
      <script type="text/javascript" src="http://www.my-website.com/js/script.js"></script>
    ...
      <!-- my page's stuff - end -->
    ... <!-- other contents... --> ...
    </html>