Call Javascript function from html file -


i have javascript code:

var cr = {};  cr.plugins_ = {};  cr.runtime = null;    cr.plugins_.vinoos_markets = function(runtime) {      this.runtime = runtime;  };  (function() {  	function initialize_events(result) {  		alert(result);  	}  })();
<button onclick="initialize_events('test result');">send result</button>

how run 'initialize_events' function html clicking on button?

i don't have access editing javascript file.

i dont have access editing js file.

then can't, full stop. it's entirely private anonymous iife* encloses it. you'd have expose global in order use onxyz-attribute-style event handler (and require modifying javascript code). it's 1 of many reasons not use them.

since can't without modifying javascript, i'm going assume overcome limitation , suggest when/if can modify javascript:

have iife hook button, , use data-* attribute if need button-specific information pass it:

var cr = {};  cr.plugins_ = {};  cr.runtime = null;    cr.plugins_.vinoos_markets = function(runtime) {    this.runtime = runtime;  };  (function() {    function initialize_events(result) {      alert(result);    }    document.getelementbyid("send-result").addeventlistener("click", function() {      initialize_events(this.getattribute("data-result"));    }, false);  }());
<button id="send-result" data-result="test result">send result</button>

notes:

  • if need support obsolete browsers without addeventlistener (such ie8, sadly still requirement many), see this answer cross-browser event hooking function.
  • if have data in iife rather button, can use directly rather using data-* attribute.
  • giving button id , using getelementbyid example; in practice, lets identify button need. can using full css selector via document.queryselector.

* iife = immediately-invoked function expression, e.g., (function() { /*...*/})(); (also called "inline-invoked function expression." erroneously called "self-invoking function," isn't; it's invoked code defining it, not function itself.)


Comments

Popular posts from this blog

python - Selenium remoteWebDriver (& SauceLabs) Firefox moseMoveTo action exception -

html - How to custom Bootstrap grid height? -

transpose - Maple isnt executing function but prints function term -