javascript-更改页面源参考文件路径
我有一个引用了javascript文件的应用程序.长话短说,出于很多原因,我无法更改此js文件,也无法更改其引用位置.因此,我想使用javsacript将参考文件路径更改为我的文件.
I have an Application that has a reference to a javascript file . Long story short, For lot of Reasons, i cannot change this js file nor can i change place where it is referred. So i want to use javsacript to change the reference file path to my file .
即:在我的应用程序中,我在页面末尾有一个引用:
ie: in my application i have a reference at the end of the page like :
<script src="FileThatIsCorrupted.js"></script>
我不能更改此文件的引用路径,也不能更改此文件的内容.
i cannot change the reference path of this file nor can i change the content of this file.
但是我还有另一个js文件,在页面顶部加载为
But i have another js file that loads at the top of the page as
<script src="FileIHaveFullControl.js"></script>
我想在我的fullControl文件中编写一个代码段,以将引用"路径更改为
i want to write a Snippet in my fullControl file to change the Reference path to
<script src="CorrectFile.js"></script>
我尝试了document.querySelectorAll('script')
和document.find('script')
但这无济于事.需要任何指针
I tried document.querySelectorAll('script')
and document.find('script')
But that is not helping. Need any pointers
假设您无权访问html文件,因此您无法对其进行编辑(否则该问题将毫无意义),抱歉,我必须通知您,脚本src只能设置一次.无法更改.
Assuming that you don't have access to the html file and thus you can't editing it (otherwise the question would make no sense), I'm affraid I have to inform you that a script src can only be set once. It's not possible to change it.
但是事实证明,您可以从dom代替.我在网上找到了此功能:
But it turns out that you can replace from the dom instead. I found this function on the web:
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"
源代码:
http://www.javascriptkit.com/javatutors/loadjavascriptcss2.shtml