简体   繁体   English

是否可以从HTML文件中的特定脚本标记引用函数或对象

[英]Is it possible to refer to function or object from particular script tag in HTML file

Imagine we have the following code in an HTML file: 想象一下,我们在HTML文件中有以下代码:

<script id='tag-1'>
  function foo(){
    alert("this is foo-1");
  }
</script>

<script id='tag-2'>
  function foo(){
    alert("this is foo-2");
  }
</script>

<script id='tag-3'>
  function foo(){
    alert("this is foo-3");
  }
</script>

Is it possible to invoke function foo from the script tag with id tag-2 ? 是否可以从id tag-2的脚本标记调用函数foo I'm just curious to know if there is any cross-browser solution? 我只是想知道是否有任何跨浏览器解决方案? Thank's guys. 多谢你们。


Ok we have two very relevant proposals - the one that I marked for accepted answer and another one of the comments rigth below this post - the one that @dfsq propose. 好的,我们有两个非常相关的提案 - 我标记为已接受答案的提案和另一个在此帖后面的评论 - @ dfsq建议的提案。 Thank you all guys!!! 谢谢大家!

No, that's not possible without resorting to eval() . 不,如果不诉诸eval() ,这是不可能的。

As each script block is evaluated by the browser the previous versions of the function are overwritten. 当浏览器评估每个脚本块时,将覆盖该函数的先前版本。

You can access the text context of each <script> tag, but only if it's inline. 可以访问每个<script>标记的文本上下文,但前提是它是内联的。 Accessing the source of <script src="..."> tags requires AJAX. 访问<script src="...">标记的源代码需要AJAX。

Yes this is totally possible, but you should use a type other than text/javascript. 是的,这是完全可能的,但你应该使用text / javascript以外的类型。

<script id='tag-1' type="myScriptHolder">
  function foo(){
    alert("this is foo-1");
  }
</script>

<script id='tag-2' type="myScriptHolder">
  function foo(){
    alert("this is foo-2");
  }
</script>

<script id='tag-3' type="myScriptHolder">
  function foo(){
    alert("this is foo-3");
  }
</script>

<script>
$.globalEval($('#tag-1').text();
</script>

No, this isn't possible. 不,这是不可能的。 All Javascript code in the same window shares the same namespace, so what you're doing in your example is simply overriding foo , meaning that in the end you only have "foo-3". 同一窗口中的所有Javascript代码共享相同的命名空间,因此您在示例中所做的只是覆盖foo ,这意味着最终您只有“foo-3”。

iframes each have their own namespace but it wouldn't make sense to use iframes for something like this. 每个iframe都有自己的命名空间,但对于像这样的东西使用iframe是没有意义的。

If the functions are only needed by their corresponding scripts, then you could wrap each script in a self-executing function, eg 如果函数只需要相应的脚本,那么您可以将每个脚本包装在一个自执行的函数中,例如

(function() {
  function foo(){
    alert("this is foo-1");
  }
})();

This would mean that foo would be inaccessible from outside that script, which obviously may or may not work for you depending on where the function is used. 这意味着foo将无法从该脚本外部访问,这显然可能会或可能不会对您有用,具体取决于函数的使用位置。

The only other alternative would be to create your own namespaces for the functions, eg: 唯一的另一种选择是为函数创建自己的命名空间,例如:

var namespace1 = {};
namespace1.foo = function foo() {...}

Javascript doesn't have function overloading. Javascript没有函数重载。 Every next declaration with the same name(variable or function) just re-declares it with new value provided. 具有相同名称(变量或函数)的每个下一个声明只需使用提供的新值重新声明它。 So in your case function from tag-3 will always be executed. 因此,在您的情况下,将始终执行tag-3中的函数。

Possible solution to what you want is 可能的解决方案是你想要的
1. Give different names for each function. 1.为每个功能指定不同的名称。
or 要么
2. If you are expecting functions to be called differently depending on arguments provided. 2.如果您希望根据提供的参数调用函数。 You can check the implicit arguments object inside the function and do necessary operations depending upon that. 您可以检查函数内部的隐式参数对象,并根据需要执行必要的操作。

Simple example of the second solution can be given as follows. 可以如下给出第二解决方案的简单示例。

function foo(bar){
    if(typeof bar==="number"){
         //Do number specific stuff here
    } else if(typeof bar==="string"){
        //Do string specific stuff here
    } else {
    }
}

You could implement a javascript namespacing technique that is fairly common. 您可以实现一种相当常见的javascript命名空间技术。 This creates a new javascript object that is available, and you can reference via calls to CodeGuru.File1.Foo(); 这将创建一个可用的新javascript对象,您可以通过对CodeGuru.File1.Foo()的调用来引用;

var CodeGuru = CodeGuru || {};  
var CodeGuru.File1 = CodeGuru.File1 || {};
(function(CodeGuru) {
    CodeGuru.File1.Foo = function() {
        //insert code
    }
 })(CodeGuru);

How about something like this: 这样的事情怎么样:

if (typeof (GenLib) == "undefined") GenLib = {};

    GenLib.Tag1 = {};
    GenLib.Tag2 = {};
    GenLib.Tag3= {};

    GenLib.Tag1.Foo = function () {
        alert("this is foo-1");
    };

    GenLib.Tag2.Foo = function () {
        alert("this is foo-2");
    };

    GenLib.Tag3.Foo = function () {
        alert("this is foo-3");
    };

    var theOneYouWantNow = GenLib.Tag2; // Or Tag1 or Tag3, use if/switch/whatever here.

    theOneYouWantNow.Foo();

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

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