javascript - call a function on chrome app -


i working chrome app , developed normal web page. includes several .js files , calls functions on click , on load.

<body onload="thisisonloadfunction()">  <button class='btn btn-info btn-large icon-undo pull-right' onclick='cancelall()'> 

they of them. not work when run app.

in console shows error saying

 refused execute inline event handler because violates following  content security policy directive: "default-src 'self'   chrome-extension-resource:".  note 'script-src' not explicitly set,  'default-src' used fallback 

how can working.

you can not use inline event handler in apps. security issue.

include js file like:

document.getelementbyid("some_id").addeventlistener("click", some_function); 

or use queryselector attach event directly ..byid("some_id").onclick etc.

typically wrap code in:

document.addeventlistener('domcontentloaded', function () {   .... }); 

as note adding event listeners.

if need pass arguments function there various ways. 1 way use closures, way find cleaner use bind. app not need vory browser capabilities on bind.

example:

function some_fun(i, e) {     console.log(i); <-- value of `i` in loop     console.log(e); <-- event. }  var p = document.getelementsbytagname('p'),     i;  (i = 0; < p.length; ++i) {     p[i].addeventlistener("click", some_fun.bind(null, i)); } 

another useful thing bind() ability pass context. example:

function myobj(x, y) {     this.x = x;     this.y = y; }  var my_obj = new myobj(42, 13);  someelem.addeventlistener("click", my_fun.bind(my_obj)); 

when someelem clicked can use things this.x , 42.


Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -