如何在不编写脚本标记的情况下执行javasrcipt代码 [英] how to execute javasrcipt code without writing script tag

查看:73
本文介绍了如何在不编写脚本标记的情况下执行javasrcipt代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只是想知道写脚本标签是否必须执行javascript代码,如果是mandoatory那么如何执行没有脚本标签的javacript代码



例子



Hi,just want to know is the mandatory to write script tag to execute javascript code ,if it is mandoatory then how to execute javacript code without script tag

example

<script > alert() </script> 

这将被执行



但如果我写的话



this will be executed

but if i write

//<script >  
alert()
//</script > 

这将不会执行



是否有任何替代脚本标记来执行javascript代码

this will not execute

is there anything alternative to script tag to excuted javascript code

推荐答案

是的,它是强制性的。你必须写这个,否则浏览器无法理解你究竟想要做什么。



简单来说,我可以说HTML是一种标记语言,这只是浏览器所理解的事情。因此,所有内容都由HTML文档中的标记表示。



当您尝试执行任何JavaScript代码时,需要使用HTML Script标记将其包装起来。这就是逻辑。
Yes, it is mandatory. You have to write that, otherwise browsers can't understand what exactly you are trying to do.

In simple words, I can say that HTML is a Markup language and that is only thing understood by the browsers. So, everything is represented by tags in a HTML document.

When you try to execute any JavaScript code, you need to wrap it with HTML Script tags. That is the logic.


为什么不在JavaScript文件中编写代码,并将其加载到HTML文件中?例如:

在test.js中:

Why don't you write the code in a JavaScript file, and load it in HTML file? For example:
In test.js:
alert('test');





在index.htm:



In index.htm:

<!DOCTYPE html>
<html>
    <body>
        <script src="test.js"></script>
    </body>
</html>


如果您正在进行内联编码,则必须包含脚本标记,否则创建一个js文件,编写代码,最后只需在页面中添加对该js文件的引用。
If you are doing inline coding, you must include script tag, or else create a js file and write your codes in that, finally just add the reference to that js file in your page.


这篇关于如何在不编写脚本标记的情况下执行javasrcipt代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆