我有一些来自外部 JS 文件的 JS,我想将它们插入到 HTML 文件中的 JS 函数中。我无法触及HTML文件中的JS脚本,所以我想知道这个方法是否可以做到。
这是我想要插入 HTML 文件中 JS 函数内部的 JS。
// FIRST JS TO INSERT
if (OS == "mobile"){
killVideoPlayer();
}
// SECOND JS TO INSERT
if (OS == "mobile"){
loadHtmlFiveVideo();
if (!document.all){
flvPlayerLoaded = false;
}
}else {
loadVideoPlayer();
}
然后我想将它插入到这里。
<script>
function mediaTypeCheck() {
if (bsiCompleteArray[arrayIndex].mediaType == "video") {
// INSERT FIRST JS HERE
document.getElementById("bsi-video-wrap").style.display = "block";
document.getElementById('pngBsi').style.display = "block";
document.getElementById("frame_photo").style.display = "none";
document.getElementById("relativeFrame").style.display = "block";
document.getElementById("buy-me-photo-button-bsi").style.display = "none";
// INSTER SECOND JS HERE
loadVideoPlayer();
}
if (bsiCompleteArray[arrayIndex].mediaType == "photo") {
killVideoPlayer();
document.getElementById("bsi-video-wrap").style.display = "none";
document.getElementById('pngBsi').style.display = "block";
document.getElementById("relativeFrame").style.display = "block";
document.getElementById("frame_photo").style.display = "block";
document.getElementById("buy-me-photo-button-bsi").style.display = "block";
if (!document.all){
flvPlayerLoaded = false;
}
}
}
</script>
谢谢!
最佳答案
在 JavaScript 中,您可以随时用新值覆盖变量,包括函数。
从表面上看,您可以将 mediaTypeCheck
函数替换为您自己的函数,该函数可以执行您需要的操作,然后调用原始函数。
例如
(function(){
// keep track of the original mediaTypeCheck
var old_function = mediaTypeCheck;
// overwrite mediaTypeCheck with your wrapper function
mediaTypeCheck = function() {
if ( conditions ) {
// do whatever you need to, then ...
}
return old_function();
};
})();
上面的内容可以从任何脚本加载,只要它在定义 mediaTypeCheck 函数之后发生即可。
关于javascript - 将外部脚本中的 javascript 插入 HTML 中的 javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12826055/