Javascript - Change The Page Source Reference File Path
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
Solution 1:
Maybe, you can try to use the holdready function of jquery like follows
$.holdReady( true );
$.getScript( "myplugin.js", function() {
$.holdReady( false );
});
then you must looking for the script element that contains an attribute src like "FileIHaveFullControl.js" and remove it.
$("script").each(function(i, j){
if($(this).attr("src").equals("corrupted file url"))
$(this).remove();
});
most information on the jquery reference http://api.jquery.com/jquery.holdready/
Solution 2:
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.
But it turns out that you can replace from the dom instead. I found this function on the web:
functioncreatejscssfile(filename, filetype){
if (filetype=="js"){ //if filename is a external JavaScript filevar fileref=document.createElement('script')
fileref.setAttribute("type","text/javascript")
fileref.setAttribute("src", filename)
}
elseif (filetype=="css"){ //if filename is an external CSS filevar fileref=document.createElement("link")
fileref.setAttribute("rel", "stylesheet")
fileref.setAttribute("type", "text/css")
fileref.setAttribute("href", filename)
}
return fileref
}
functionreplacejscssfile(oldfilename, newfilename, filetype){
var targetelement=(filetype=="js")? "script" : (filetype=="css")? "link" : "none"//determine element type to create nodelist usingvar targetattr=(filetype=="js")? "src" : (filetype=="css")? "href" : "none"//determine corresponding attribute to test forvar allsuspects=document.getElementsByTagName(targetelement)
for (var i=allsuspects.length; i>=0; i--){ //search backwards within nodelist for matching elements to removeif (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"
SOURCE CODE:
http://www.javascriptkit.com/javatutors/loadjavascriptcss2.shtml
Post a Comment for "Javascript - Change The Page Source Reference File Path"