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,... ?

1
  • I think you got all the ways I know of Commented Aug 17, 2012 at 14:58

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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.
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.
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.

3 Comments

Even if you're not wanting to hide your javascript, it's still another way of calling a javascript function/file.
You want obfuscation, this example shown to me by a coleague left me perplexed: patriciopalladino.com/blog/2012/08/09/…, generate a code with this patriciopalladino.com/files/hieroglyphy and try to eval that in console, I was amazed.
@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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.