Why don't self-closing script elements work?

前端 未结 12 2613
攒了一身酷
攒了一身酷 2020-11-21 04:12

What is the reason browsers do not correctly recognize:



        
12条回答
  •  -上瘾入骨i
    2020-11-21 04:43

    The self closing script tag won't work, because the script tag can contain inline code, and HTML is not smart enough to turn on or off that feature based on the presence of an attribute.

    On the other hand, HTML does have an excellent tag for including references to outside resources: the tag, and it can be self-closing. It's already used to include stylesheets, RSS and Atom feeds, canonical URIs, and all sorts of other goodies. Why not JavaScript?

    If you want the script tag to be self enclosed you can't do that as I said, but there is an alternative, though not a smart one. You can use the self closing link tag and link to your JavaScript by giving it a type of text/javascript and rel as script, something like below:

    
    

提交回复
热议问题