0

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

4
  • What kind of ifs? in php? Show us some code tis difficult to know what you are asking. Commented Mar 14, 2012 at 19:39
  • please post relevant javascript and html, this question is too vague to answer. Commented Mar 14, 2012 at 19:39
  • Could you explain how just calling a function isn't sufficient for your needs? That is, "if (x) { functionA(); } else { functionB(); }". Commented Mar 14, 2012 at 19:39
  • Please show us a HTML-snippet where you use <IF>. Commented Mar 14, 2012 at 19:40

3 Answers 3

1

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.

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

2 Comments

In this case, I want to call myFunction(); wich is in an external .js document. Can I call this function this way?
As long as you include your js file in the document BEFORE your function declaration. In your case, I would recommend making the script call in the <head> element.
1

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>

Comments

1

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

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

Comments

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.