javascriptonloadcreate-function

Create JavaScript function on onload image


Is it possible to create a function in the onload event of an image? Is there a method that makes this possible?

<input type='text' name='data' onblur='myFunction(this.value)'>
<img src='img/none.gif' onload="function myFunction(text){alert(text)}">

the image and field will be loaded by an ajax function.


Solution

  • Your code is using a function declaration, so you are defining the myFunction variable only within the scope of the onload function (so it isn't in scope when you try to call it from the onblur function).

    So you can, you just need to explicitly assign it to be a global variable.

    <input type='text' name='data' onblur='myFunction(this.value)'>
    <img src='https://static.jsbin.com/images/dave.min.svg' onload="window.myFunction = function myFunction(text){alert(text)}">

    … I can't think of any good use case for this though.