user3350003 user3350003 - 1 year ago 129
Javascript Question

Javascript immediate function call from external function

I am trying to call the immediate function defined in test1.js on click of the button defined under html file. It always throws error "test is undefined". I am little bit aware that being a immediate function, it calls immediately, and so it returns the "undefined error". But is there any way I can call the immediate function (access methods, properties, etc.) on click of the button?

Thank you in advance.


var test = (function(){
var tmp = 'hello';

<!DOCTYPE html>

<script type="text/javascript" src="test1.js"></script>

<input type="button" id="btn1" value="ClickMe!" />

<script type="text/javascript">
var btn = document.getElementById("btn1");

function fun1(){


Answer Source

You have to modify your code so that the IIFE returns an object with a tmp property. Such as

var test = (function(){
var tmp = 'hello';
return {tmp:tmp};
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download