本文介绍了内联onclick处理程序中可以执行的操作是否有任何限制?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在将代码添加到onclick处理程序时,可以做些什么限制?

我是否可以使用以分隔的无限制语句? ?
我可以声明将存在于全局范围内的变量吗?
我可以在其中声明一个匿名函数并调用它吗? (function(){})();?
有什么限制吗?

我问的原因是我将大量代码放在onclick处理程序中,并且仅在IE中,此后没有找到我声明并应进入全局范围的变量. /p>

但是,相同的代码在其他浏览器中也可以使用,当我将相同的代码用作书签时,它在IE中也可以使用.

有问题的代码在这里

我之所以制作如此恐怖而笨重的onclick处理程序,是因为我正在编写其他人可以在其网站上放置的代码(认为:Digg按钮),这将注入我的脚本.我试图使它们保持简单,而又不会使它们的名称空间过于混乱,这就是为什么只给他们一个< img>使用onclick处理程序是理想的选择.
如果我绝对无法完成这项工作,则必须给他们提供代码,该代码声明一个< script>并在其中添加一个命名函数,然后再提供< img>使用调用该函数的onclick处理程序.

有什么想法吗?

谢谢!

解决方案

尝试在window.之前添加到AppD.

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!

解决方案

Try prepending window. to AppD.

这篇关于内联onclick处理程序中可以执行的操作是否有任何限制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 06:41