petehare petehare - 1 year ago 92
Javascript Question

Passing a Javascript function through inline data- attributes

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
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:

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

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

I've tried changing the
attribute to
, still had no luck...

Answer Source

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

//for example, a global function

//a namespaced function
//similar to doing ns.someFunctionName()
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download