Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/387.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-更改页面源引用文件路径_Javascript_Jquery - Fatal编程技术网

javascript-更改页面源引用文件路径

javascript-更改页面源引用文件路径,javascript,jquery,Javascript,Jquery,我有一个引用javascript文件的应用程序。长话短说,由于很多原因,我不能更改这个js文件,也不能更改引用它的位置。因此,我想使用javascript更改文件的参考文件路径 ie:在我的申请表中,我在页面末尾有一个参考资料,如: <script src="FileThatIsCorrupted.js"></script> 我无法更改此文件的引用路径,也无法更改此文件的内容 但我有另一个js文件,在页面顶部加载为 <script src="FileIHa

我有一个引用javascript文件的应用程序。长话短说,由于很多原因,我不能更改这个js文件,也不能更改引用它的位置。因此,我想使用javascript更改文件的参考文件路径

ie:在我的申请表中,我在页面末尾有一个参考资料,如:

   <script src="FileThatIsCorrupted.js"></script>
我无法更改此文件的引用路径,也无法更改此文件的内容

但我有另一个js文件,在页面顶部加载为

 <script src="FileIHaveFullControl.js"></script>
我想在fullControl文件中编写一个代码段,将引用路径更改为

<script src="CorrectFile.js"></script>
我试过document.querySelectorAll'script'和document.find'script'
但这没有帮助。需要任何指针

也许,您可以尝试使用jquery的holdready函数,如下所示

$.holdReady( true );
$.getScript( "myplugin.js", function() {
   $.holdReady( false );
});
然后必须查找包含属性src(如FileIHaveFullControl.js)的脚本元素,并将其删除

$("script").each(function(i, j){
    if($(this).attr("src").equals("corrupted file url"))
        $(this).remove(); 
});

jquery参考中的大多数信息假定您没有访问html文件的权限,因此无法编辑它,否则问题就没有意义了,我很抱歉,我必须通知您,脚本src只能设置一次。不可能改变它

但事实证明,您可以从dom中替换。我在网上找到了这个功能:

function createjscssfile(filename, filetype){
 if (filetype=="js"){ //if filename is a external JavaScript file
  var fileref=document.createElement('script')
  fileref.setAttribute("type","text/javascript")
  fileref.setAttribute("src", filename)
 }
 else if (filetype=="css"){ //if filename is an external CSS file
  var fileref=document.createElement("link")
  fileref.setAttribute("rel", "stylesheet")
  fileref.setAttribute("type", "text/css")
  fileref.setAttribute("href", filename)
 }
 return fileref
}

function replacejscssfile(oldfilename, newfilename, filetype){
 var targetelement=(filetype=="js")? "script" : (filetype=="css")? "link" : "none" //determine element type to create nodelist using
 var targetattr=(filetype=="js")? "src" : (filetype=="css")? "href" : "none" //determine corresponding attribute to test for
 var allsuspects=document.getElementsByTagName(targetelement)
 for (var i=allsuspects.length; i>=0; i--){ //search backwards within nodelist for matching elements to remove
  if (allsuspects[i] && allsuspects[i].getAttribute(targetattr)!=null && allsuspects[i].getAttribute(targetattr).indexOf(oldfilename)!=-1){
   var newelement=createjscssfile(newfilename, filetype)
   allsuspects[i].parentNode.replaceChild(newelement, allsuspects[i])
  }
 }
}

replacejscssfile("oldscript.js", "newscript.js", "js") //Replace all occurences of "oldscript.js" with "newscript.js"
replacejscssfile("oldstyle.css", "newstyle", "css") //Replace all occurences "oldstyle.css" with "newstyle.css"
源代码:


客户端JS无法写入文件。您可以更改脚本源my删除脚本,创建并添加一个新的脚本,并将修改后的src路径添加到DOMIs。无论如何,我们可以更改Corruptedfile.js的内容?不完全是这样。但有一个解决办法。看看我的答案。我编辑了id。我检查了这个。但我不确定这是否有效,因为损坏的文件包含document.ready代码,并且在删除文件引用之前将加载该代码:在这种情况下,仍然有希望。检查你问题的另一个答案。它提到了一个jquery函数来保存documentready$.holdReady。您也可以在提供的链接中找到有关该方法的更多信息。它基本上是说你必须在head标签之后应用这个函数。