admin管理员组文章数量:1389862
I have created a DOM structure like this
<div data-execute="someFunction.abc" id="someId">
</div>
I am able to retrive the attribute in js but I intend to execute this as a callback function. So I am doing like this
var x = document.getElementById("someId").getAttribute('data-execute');
As expected this is returning someFunction.abc .But on consoling typeof(x) it is showing "string".Please refer to this fiddle
var someFunction = function() {
alert("Hello")
}
var load = (function(module, global) {
var x = document.getElementById("someId").getAttribute('data-execute');
console.log(typeof(x))
}(load || {}, this))
<div data-execute="someFunction.abc" id="someId">
Some Function
</div>
I have created a DOM structure like this
<div data-execute="someFunction.abc" id="someId">
</div>
I am able to retrive the attribute in js but I intend to execute this as a callback function. So I am doing like this
var x = document.getElementById("someId").getAttribute('data-execute');
As expected this is returning someFunction.abc .But on consoling typeof(x) it is showing "string".Please refer to this fiddle
var someFunction = function() {
alert("Hello")
}
var load = (function(module, global) {
var x = document.getElementById("someId").getAttribute('data-execute');
console.log(typeof(x))
}(load || {}, this))
<div data-execute="someFunction.abc" id="someId">
Some Function
</div>
I also checked this link Passing a Javascript function through inline data- attributes
But no way I am able to execute it as a call back function.Any help will be truly appreciable.
Share Improve this question edited Apr 19, 2018 at 10:20 brk asked Sep 26, 2015 at 8:39 brkbrk 50.4k10 gold badges59 silver badges84 bronze badges 1- 1 What is abc? It's a paremeter? – Irvin Dominin Commented Sep 26, 2015 at 8:51
3 Answers
Reset to default 2Try this:
<div data-execute="someFunction.abc" id="someId"></div>
var x = document.getElementById("someId").getAttribute('data-execute');
window[x].call();
You can use the call
methodon the function defined in the global scope, you can access it in the global window ojbect.
Ref:
The call() method calls a function with a given this value and arguments provided individually.
I have assumed the code after the point is a paramter to pass to the function.
Code:
var someFunction = function (p) {
alert(p)
}
var load = (function (module, global) {
var x = document.getElementById("someId").getAttribute('data-execute');
window[x.split('.')[0]].call(undefined, x.split('.')[1]);
}(load || {}, this))
Demo: https://jsfiddle/IrvinDominin/5bjsmu3x/
I was struggling with the same question and I found this solution :
HTML :
<element data-call="return alert('callback');">
JS :
Function(YourElement.getAttribute('data-callback'))();
You can store it in a variable and add parameters too :
HTML :
<element data-call="return str.toUpperCase();">
JS :
var fn = Function("str", YourElement.getAttribute('data-callback'));
var returned = fn("Test String");
本文标签: javascriptPass a callback function as a html data attributeStack Overflow
版权声明:本文标题:javascript - Pass a callback function as a html data attribute - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744594060a2614668.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论