最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

html - Passing a Javascript function through inline data- attributes - Stack Overflow

programmeradmin4浏览0评论

I'm working with a Javascript file upload library, and one of it's features is that it uses HTML5 inline data- attributes to pass information to the plugin.

This is working great for anything data related, strings, numbers etc., however the plugin has some callback methods you can assign function to. My problem is that when trying to pass a javascript function through these inline data attributes, like this:

<input type="file" name="test" data-on-finish="alert();">

The plugin picks up the reference to the onFinish() callback method fine, but when it tries to execute whatever javascript I put in there I get the error:

Uncaught TypeError: Object alert(); has no method 'call' 

I'm assuming it's reading the alert(); as a string. Any idea how I can pass through executable javascript to the plugin?

I believe the plugin I'm using is an extension to the jQuery file upload plugin:

Update: I've also tried using globally defined functions, like this:

<script type="text/javascript">
    function myTesting(){
       alert('yay');
    }
</script>
<input type="file" name="test" data-on-finish="myTesting">

I've tried changing the data-on-finish attribute to myTesting, myTesting(), still had no luck...

I'm working with a Javascript file upload library, and one of it's features is that it uses HTML5 inline data- attributes to pass information to the plugin.

This is working great for anything data related, strings, numbers etc., however the plugin has some callback methods you can assign function to. My problem is that when trying to pass a javascript function through these inline data attributes, like this:

<input type="file" name="test" data-on-finish="alert();">

The plugin picks up the reference to the onFinish() callback method fine, but when it tries to execute whatever javascript I put in there I get the error:

Uncaught TypeError: Object alert(); has no method 'call' 

I'm assuming it's reading the alert(); as a string. Any idea how I can pass through executable javascript to the plugin?

I believe the plugin I'm using is an extension to the jQuery file upload plugin: https://github.com/blueimp/jQuery-File-Upload/wiki/Options

Update: I've also tried using globally defined functions, like this:

<script type="text/javascript">
    function myTesting(){
       alert('yay');
    }
</script>
<input type="file" name="test" data-on-finish="myTesting">

I've tried changing the data-on-finish attribute to myTesting, myTesting(), still had no luck...

Share Improve this question edited Jun 23, 2012 at 1:43 petehare asked Jun 23, 2012 at 0:20 peteharepetehare 1,86516 silver badges14 bronze badges 2
  • Gave it a try, no luck. I've added the update above. – petehare Commented Jun 23, 2012 at 1:43
  • I think the eval() will do the job but can be dangerous : stackoverflow.com/questions/86513/… – user1705628 Commented Sep 28, 2012 at 7:56
Add a comment  | 

3 Answers 3

Reset to default 20

why not place the name of the callback instead? something like

//for example, a global function
window['someFunctionName'].call();

//a namespaced function
//similar to doing ns.someFunctionName()
ns['someFunctionName'].call();

It looks like it is trying to call a function back, as in, myfunction.call(

Try passing is a function like this.

<input type="file" name="test" data-on-finish="myFunction;">

function myFunction(){
 alert('I am alerting');
};

Use something like this:

$(function() {
  function test() {
    alert("ok");
  }

  $(".test").data("test", test);
  $(".test").data("test")();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<div class="test">
</div>

发布评论

评论列表(0)

  1. 暂无评论