简体   繁体   English

什么是全球范围<script> tag?

[英]What is the global scope of <script> tag?

I had made flag and made my previous question deleted because of missunderstanding. 由于误解,我制作了旗帜并删除了我之前的问题。

I'm working on a classic asp project. 我正在研究一个经典的asp项目。

let's say you have so many <script></script> tags in your code. 假设您的代码中有很多<script></script>标记。

For instance: 例如:

line 10: <script> ..function 1 definition here..</script> 第10行: <script> ..function 1 definition here..</script>

line 200: <script> .. function 2 definition here..</script> 第200行: <script> .. function 2 definition here..</script>

line 5000: <script> ..function 3 definition here..</script> 第5000行: <script> ..function 3 definition here..</script>

also at line 6000: I have another tag which is trying to call function1. 也在第6000行:我有另一个试图调用function1的标签。

is that possible without using *.js file ? 可以不使用* .js文件吗?

For instance: 例如:

line 6000: 第6000行:

<script> function1(); </script>

Those scripts are not defined in <head> tag. 这些脚本未在<head>标记中定义。

I know its not useful but I need to know is there any way of it or not. 我知道它没用,但我需要知道它有没有任何方法。

Hope its more clear now! 希望现在更加清晰!

anything inside the script tags gets run immediately. 脚本标记内的任何内容都会立即运行。 if you define function a() in your first script element, then it will add a function called a to your global namespace. 如果在第一个脚本元素中定义function a() ,那么它将向您的全局命名空间添加一个名为a的函数。 any javascript you execute later on in other script elements will have access to it. 您稍后在其他脚本元素中执行的任何JavaScript都可以访问它。

<script type="text/javascript">
   function a() {
       alert('hi');
   }
</script>

...

<script type="text/javascript">
    a();
</script>

是的,这是可能的,假设function1在全局范围内(例如,不在包装函数/自调用函数中)。

Of course it is possible . 当然有可能。 You just need to define it in global namespace. 您只需要在全局命名空间中定义它。 Here is the link which should give you an idea and better understanding. 是一个链接,应该给你一个想法和更好的理解。 It also includes very simple examples. 它还包括非常简单的例子。

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

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