1

How many ways are to call a JavaScript code? First there is the typical <script> tag then there are the attribute of some tags, onload="" for the body tag, href="JavaScript:" of a tag, onSubmit="JavaScript:" of form tag, the onclick= attribute.

Beside this I know that code can also be loaded from JavaScript after all is loaded: How to include JavaScript file or library in Chrome console?.

But I am more interested in the ways code can be run from the html document, is there an exhaustive list: events, attributes,... ?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Eduard Florinescu
  • 16,747
  • 28
  • 113
  • 179

2 Answers2

5

Here is a good list of events in JavaScript: http://help.dottoro.com/larrqqck.php

As far as how to trigger these events, you have covered all of the bases that I know of. There is a different way to use the onload (and similar events), like this:

Plain JavaScript:

window.onload = function() {
  //Do Stuff
}

jQuery:

$(window).bind('onload', function() {
  //Do stuff
});

Side note: I would discourage the use of embedding JavaScript within attributes like href="javascript:" or onsubmit="javascript:". Embedding JavaScript in HTML like this is considered poor practice, since it makes your code less readable, and can be more difficult to maintain.

If you need to listen for events like onsubmit, I would recommend listening for these events in JavaScript/jQuery, or by calling a JavaScript function, like this: onsubmit="javascript:myFunction(parameter)", rather than writing all of the JavaScript inline with the HTML. That's my two cents.

Hope that is helpful.

Oliver Spryn
  • 16,871
  • 33
  • 101
  • 195
  • Why I am asking this has another reason, the trend is now with html5 apps, suppose you want to automatically check conformance of an html5 app and you need to check exactly this where code is not recommended to be. Also there are a lot of reasons to know this cases, both practical and didactic. – Eduard Florinescu Aug 17 '12 at 15:13
  • 1
    True. Excellent question. HTML5 is trending toward heavier use of JavaScript/AJAX technologies. IMHO, `onsubmit="javascript:"` has always been the lazy way of handling JS events. I've never seen a modern HTML5 web application use this method for handling events. More and more apps use the jQuery approach listed above, since many modern HTML5 sites use frameworks such as jQuery. So, if you are looking for my opinion, I would stick with this approach. – Oliver Spryn Aug 17 '12 at 15:19
2

I recommend you look into this post. Basically, there are some mentions server side calling of these functions to hide your javascript from the average user.

Community
  • 1
  • 1
Shabab
  • 249
  • 1
  • 9
  • Even if you're not wanting to hide your javascript, it's still another way of calling a javascript function/file. – Shabab Aug 17 '12 at 15:04
  • You want obfuscation, this example shown to me by a coleague left me perplexed: http://patriciopalladino.com/blog/2012/08/09/non-alphanumeric-javascript.html, generate a code with this http://patriciopalladino.com/files/hieroglyphy/ and try to eval that in console, I was amazed. – Eduard Florinescu Aug 17 '12 at 15:25
  • @Eduard Florinescu. Yes: I was personally looking for obfuscation, yes. However, I also learned that server-side scripts can call JavaScript files/functions, which actually pertains to the main question here. – Shabab Aug 17 '12 at 15:35