Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How many Different ways to invoke JS inside a html Document?

So far I know, 4 different ways to define & invoke JavaScript in webpage

1.inline JS

<script type='text/javascript'> ... </script>

2.External JS

<script src="someURL"></script>

3.Event hander JS:

<input type="button" onclick="...javascript..."         

4.JavaScript:URL

<a href="javascript: ...JS CODE...">js</a>

Is there any other ways JS can be inserted in a webpage ? Has it (how many ways) been defined in any standard Specification?

Is there any difference in the execution context of JavaScript among the mentioned 4 different ways?

like image 235
Prosunjit Biswas Avatar asked Jun 13 '12 17:06

Prosunjit Biswas


2 Answers

There's actually quite a lot of ways to execute Javascript in HTML, all varying across browsers and platforms.

A lot of them (but not all) are listed in this infamous XSS cheatsheet.

Among less obscure ones, there are these:

<img src="javascript:...">
<body background="javascript:...">
<style>BODY{-moz-binding:url("...")}</style>
<META HTTP-EQUIV="refresh" CONTENT="0;url=javascript:...">

...and so on.

like image 95
kangax Avatar answered Nov 09 '22 02:11

kangax


1 and 2 are basically the same, just that 2 is an external file and 1 has the content right there.

3 and 4 are along similar lines to eval. 3 defines this to be the element the event is on.

That's about it. If there are any other ways to include JS, I've never heard of them.

like image 29
Niet the Dark Absol Avatar answered Nov 09 '22 01:11

Niet the Dark Absol