mauguerra
mauguerra

Reputation: 3858

calling a javascript function without an element

I know it sounds a little bit strange, but I want a javascript function to be executed without being called by an onclick() onblur() onchange or anything like this.

My program is divided by many IFs. If a certain IF is executed, then I know that the javascript function should be executed, but there is not any HTML element that calls it.

This is in HTML.

Any idea?

Thanks

Upvotes: 0

Views: 419

Answers (3)

Mike
Mike

Reputation: 364

Use a pub/sub pattern. JQuery has on built in

https://gist.github.com/1321768 http://addyosmani.com/resources/essentialjsdesignpatterns/book/#detailedobserver

Upvotes: 1

ShankarSangoli
ShankarSangoli

Reputation: 69905

Just call the function anywhere you want to execute it. If it is part of html i.e inline script then make sure you have used script tags so that it will be treated as script by the browser.

<script type="text/javascript">
   functionName();
</script>

Upvotes: 1

Nick Beranek
Nick Beranek

Reputation: 2751

If you want to do inline JavaScript, then you can do the following in your HTML:

<script type="text/javascript">myFunction();</script>

That will call the function without needing an event listener.

Upvotes: 1

Related Questions