javascript - Can I create my own event attributes? -
can create own attributes on html elements treated javascript code, , able executed when code desires?
for example, want create custom event onpagerefresh
, , @ same time, allow person writes html add attributes handle event.
<body onpagerefresh="updatecontents()">
i able parse html value of new onpagerefresh
attribute, how tell javascript treat in attribute javascript code rather string?
you can use new function()
, function constructor, function object string , assign attribute of object. ex:
var divelement = document.getelementbyid('mydiv'); divelement.onpagerefresh = new function("èvent", "alert();");
whether that's better eval() in meaningful way debatable (though give distinct scope), how it. possibly better option in case allow person implementing html use preexisting function name (onpagerefresh="updatecontents"
) , execute 1 e.g. window["updatecontents"]()
. way don't let old dom insertion write random javascript code. of course limits functionality.
Comments
Post a Comment