Are there any limitations to what can be done in an inline onclick handler?

那年仲夏 提交于 2019-12-24 06:33:35

问题


When adding code to an onclick handler, are there any limitations to what can be done?

Can I have unlimited statements separated by ; ?
Can I declare variables that will live in the global scope?
Can I declare an anonymous function right in there and call it? (function(){})();?
Is there any limitation?

The reason I'm asking is that i'm putting a lot of code in an onclick handler, and only in IE, one variable that I'm declaring and should go to the global scope is not being found afterwards.

However, that same code does work in other browsers, and it does work in IE when I use the same code as a bookmarklet.

EDIT: The code in question is here

EDIT 2: The reason I'm making such a horrible and bulky onclick handler is that I'm making code that other people can put on their websites (think: Digg button), that will inject a script of mine. I'm trying to keep it simple for them, without cluttering their namespace too much, which is why giving them simply an <img> with an onclick handler is ideal.
If I absolutely can't make this work, then I'll have to give them code that declares a <script>, with a named function inside, and then the <img> with the onclick handler that calls that function.

Any ideas?

Thanks!


回答1:


Try prepending window. to AppD.



来源:https://stackoverflow.com/questions/861135/are-there-any-limitations-to-what-can-be-done-in-an-inline-onclick-handler

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!