简体   繁体   English

将外部脚本中的javascript插入HTML中的javascript

[英]Insert javascript from an external script into javascript in the HTML

I have some JS from an external JS file that I want to insert inside of a JS function in the HTML file. 我有一些来自外部JS文件的JS,我希望在HTML文件的JS函数中插入。 I can not touch the JS script in the HTML file, so I am wondering if this method can be done. 我无法触摸HTML文件中的JS脚本,所以我想知道这个方法是否可以完成。

Here is the JS I want to insert inside of the JS function in the HTML file. 这是我想在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();
    }

Then I want to insert it into here. 然后我想把它插入到这里。

<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>

Thank you! 谢谢!

The easiest way for me in the past has been using server-side includes. 对我来说最简单的方法是使用服务器端包含。 Depending on your back end, you can set up a PHP or ASP page or whatever to respond with a mime type that mimics ".js". 根据您的后端,您可以设置PHP或ASP页面或其他任何响应模仿“.js”的mime类型。

I'm not a PHP guy, but you'd do something like this: (if my syntax is incorrect, please someone else fix it) 我不是一个PHP人,但你会做这样的事情:(如果我的语法不正确,请其他人修复它)

<?php 
//adding this header will make your browser think that this is a real .js page
header( 'Content-Type: application/javascript' );
?>

//your normal javascript here
<script>
function mediaTypeCheck() {
if (bsiCompleteArray[arrayIndex].mediaType == "video") {

//here is where you would 'include' your first javascript page
<?php 
      require_once(dirname(__FILE__) . "/file.php");
?>

//now continue on with your normal javascript code
document.getElementById("bsi-video-wrap").style.display = "block";
document.getElementById('pngBsi').style.display = "block";
.......

In JavaScript, you can overwrite variables with new values at any time, including functions. 在JavaScript中,您可以随时使用新值覆盖变量,包括函数。

By the looks of it, you could replace the mediaTypeCheck function with one of your own that does what you need and then calls the original function. 通过它的外观,您可以用您自己的一个替换mediaTypeCheck函数,然后调用原始函数。

Eg 例如

(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();
  };
})();

The above can be loaded from any script, so long as it happens after the mediaTypeCheck function is defined. 只要在定义了mediaTypeCheck函数后发生,就可以从任何脚本加载上述内容。

You cannot insert JS inside JS. 你不能在JS中插入JS。 What you can do is insert another tag into the DOM and specify the SRC for the external JS file. 可以做的是在DOM中插入另一个标签,并为外部JS文件指定SRC。

You can directly insert js file using $.getScript(url) ; 您可以使用$.getScript(url)直接插入js文件;

if you have script as text then you can create script tag. 如果您将脚本作为文本,那么您可以创建脚本标记。

var script = docuent.createElement('script');
script.innerText = text;
document.getElementsByTagName('head')[0].appendChild(script);

The issue in your case is that you cannot touch the script in the html, so I'll say that it cannot be done on the client side. 在你的情况下的问题是你不能触摸HTML中的脚本,所以我会说它不能在客户端完成。

If you could at least touch the script tag (not the script itself), then you could add a custom type to manipulate it before it executes, for example: 如果您至少可以触摸脚本标记(而不是脚本本身),那么您可以添加自定义类型以在其执行之前对其进行操作,例如:

<script type="WaitForInsert">

Your question looks some strange, but seems to be possible. 你的问题看起来很奇怪,但似乎有可能。 Try my quick/dirty working code and implement your own situation: 尝试我的快速/脏工作代码并实现您自己的情况:

$(document.body).ready(function loadBody(){
var testStr = test.toString();
    var indexAcc = testStr.indexOf("{");
    var part1 = testStr.substr(0, indexAcc + 1);
    var part2 = testStr.substr(indexAcc + 1, testStr.length - indexAcc - 2);
    var split = part2.split(";");
    split.pop();
    split.splice(0,0, "alert('a')");
    split.splice(split.length -1, 0, "alert('d')");
    var newStr = part1 + split.join(";") + ";" + "}";
    $.globalEval(newStr);
    test();
}
function test(){
    alert('b');
    alert('c');
    alert('e');
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM