源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

元素的内联事件处理函数的特殊作用域在各浏览器中存在差异

  • 时间:2020-09-28 12:01 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:元素的内联事件处理函数的特殊作用域在各浏览器中存在差异
[h1]标准参考[/h1] 无。 [h1]问题描述[/h1] 在一个元素的属性中绑定事件,实际上就创建了一个[b]内联事件处理函数[/b](如<h1 onclick="alert(this);"...>...</h1>),[b]内联事件处理函数[/b]有其特殊的作用域链,并且各浏览器的实现细节也有差异。 [h1]造成的影响[/h1] 如果在元素的[b]内联事件处理函数[/b]中使用的变量或调用的方法不当,将导致脚本运行出错。 [h1]受影响的浏览器[/h1]
所有浏览器
[h1]问题分析[/h1] [h2]1. 内联事件处理函数的作用域链[/h2] 与其他函数不同,[b]内联事件处理函数[/b]的作用域链从头部开始依次是:调用对象、该元素的 DOM 对象、该元素所属 FORM 的 DOM 对象(如果有)、document 对象、window 对象(全局对象)。 如以下代码:
<form action="." method="get">
 <input type="button" value="compatMode" onclick="alert(compatMode);">
</form>
相当于1
<form action="." method="get">
 <input type="button" value="compatMode">
</form>
<script>
document.getElementsByTagName("input")[0].onclick=function(){
 with(document){
  with(this2.form)3{
   with(this2){
    alert(compatMode);
   }
  }
 }
}
</script>
以上两种写法的代码在所有浏览器中都将弹出 document.compatMode 的值。 将上述代码中的 'compatMode' 替换为 'method',则在各浏览器中都将弹出 'get',即 INPUT 元素所在表单对象的 method 属性值。 注: 1. 这段代码仅为说明问题而模拟各浏览器的行为,并非表示所有浏览器都是如此实现的。 2. 是使用 this 关键字还是直接使用这个 DOM 对象,在各浏览器中有差异,详情请看本文 2.1 中的内容。 3. 是否添加 FORM 对象到作用域链中,各浏览器在实现上也有差异,详情请看本文 2.2 中的内容。 [h2]2. 内联事件处理函数的作用域链在各浏览器中的差异[/h2] 参考 WebKit 的源码:
void V8LazyEventListener::prepareListenerObject(ScriptExecutionContext* context)
{
  if (hasExistingListenerObject())
    return;

  v8::HandleScope handleScope;

  V8Proxy* proxy = V8Proxy::retrieve(context);
  if (!proxy)
    return;

  // Use the outer scope to hold context.
  v8::Local<v8::Context> v8Context = worldContext().adjustedContext(proxy);
  // Bail out if we cannot get the context.
  if (v8Context.IsEmpty())
    return;

  v8::Context::Scope scope(v8Context);

  // FIXME: cache the wrapper function.

  //[b]Nodes other than the document object, when executing inline event handlers push document, form, and the target node on the scope chain.[/b]
  // We do this by using 'with' statement.
  // See chrome/fast/forms/form-action.html
  //   chrome/fast/forms/selected-index-value.html
  //   base/fast/overflow/onscroll-layer-self-destruct.html
  //
  // Don't use new lines so that lines in the modified handler
  // have the same numbers as in the original code.
  String code = "(function (evt) {" \
      "with ([b]this[/b].ownerDocument ?[b]this[/b].ownerDocument : {}) {" \
      "with ([i][b]this[/b].form[/i] ?[b]this[/b].form : {}) {" \
      "with ([b]this[/b]) {" \
      "return (function(evt){";
  code.append(m_code);
  // Insert '\n' otherwise //-style comments could break the handler.
  code.append( "\n}).call(this, evt);}}}})");
  v8::Handle<v8::String> codeExternalString = v8ExternalString(code);
  v8::Handle<v8::Script> script = V8Proxy::compileScript(codeExternalString, m_sourceURL, m_lineNumber);
  if (!script.IsEmpty()) {
    v8::Local<v8::Value> value = proxy->runScript(script, false);
    if (!value.IsEmpty()) {
      ASSERT(value->IsFunction());

      v8::Local<v8::Function> wrappedFunction = v8::Local<v8::Function>::Cast(value);

      // Change the toString function on the wrapper function to avoid it
      // returning the source for the actual wrapper function. Instead it
      // returns source for a clean wrapper function with the event
      // argument wrapping the event source code. The reason for this is
      // that some web sites use toString on event functions and eval the
      // source returned (sometimes a RegExp is applied as well) for some
      // other use. That fails miserably if the actual wrapper source is
      // returned.
      DEFINE_STATIC_LOCAL(v8::Persistent<v8::FunctionTemplate>, toStringTemplate, ());
      if (toStringTemplate.IsEmpty())
        toStringTemplate = v8::Persistent<v8::FunctionTemplate>::New(v8::FunctionTemplate::New(V8LazyEventListenerToString));
      v8::Local<v8::Function> toStringFunction;
      if (!toStringTemplate.IsEmpty())
        toStringFunction = toStringTemplate->GetFunction();
      if (!toStringFunction.IsEmpty()) {
        String toStringResult = "function ";
        toStringResult.append(m_functionName);
        toStringResult.append("(");
        toStringResult.append(m_isSVGEvent ? "evt" : "event");
        toStringResult.append(") {\n ");
        toStringResult.append(m_code);
        toStringResult.append("\n}");
        wrappedFunction->SetHiddenValue(V8HiddenPropertyName::toStringString(), v8ExternalString(toStringResult));
        wrappedFunction->Set(v8::String::New("toString"), toStringFunction);
      }

      wrappedFunction->SetName(v8::String::New(fromWebCoreString(m_functionName), m_functionName.length()));

      setListenerObject(wrappedFunction);
    }
  }
}
从以上代码可以看出,WebKit 在向作用域链中添加对象时,使用了 'this' 关键字,并且通过判断 'this.form' 是否存在来决定是否添加 FORM 对象到作用域链中。 其他浏览器中也有类似的实现方式,但在各浏览器中,将目标对象(即绑定了此[b]内联事件处理函数[/b]的对象)添加到作用域链中的方式有差异,判断并决定是否在作用域链中添加 FORM 对象的方法也不相同。 [h3]2.1. 各浏览器在生成这个特殊的作用域链时添加目标对象时使用的方法不同[/h3] 各浏览器都会将[b]内联事件处理函数[/b]所属的元素的 DOM 对象加入到作用域链中,但加入的方式却是不同的。 如以下代码:
<input type="button" value="hello" onclick="alert(value);">
在所有浏览器中,都将弹出 'hello'。 再修改代码以变更 INPUT 元素的[b]内联事件处理函数[/b]的执行上下文:
<input type="button" value="hello" onclick="alert(value);">
<script>
var $target=document.getElementsByTagName("input")[0];
var o={
 onclick:$target.onclick,
 value:"Hi, I'm here!"
};
o.onclick();
</script>
在各浏览器中运行的结果如下:
IE Chrome Hi, I'm here!
Firefox Safari Opera hello
可见,各浏览器将[b]内联事件处理函数[/b]所属的元素的 DOM 对象加入到作用域链中的方式是不同的。 在 IE Chrome 中的添加方式类似以下代码:
<input type="button" value="hello">
<script>
var $target=document.getElementsByTagName("input")[0];
$target.onclick=function(){
 with(document){
  with(this){
   alert(value);
  }
 }
}
</script>
而在 Firefox Safari Opera 中的添加方式则类似以下代码:
<input type="button" value="hello">
<script>
var $target=document.getElementsByTagName("input")[0];
$target.onclick=function(){
 with(document){
  with($target){
   alert(value);
  }
 }
}
</script>
由于极少需要改变[b]内联事件处理函数[/b]的执行上下文,这个差异造成的影响并不多见。 [h3]2.2. 各浏览器在生成这个特殊的作用域链时对于在何种情况下添加 FORM 对象有不同理解[/h3] 各浏览器都会将[b]内联事件处理函数[/b][i]所属[/i]的 FORM 对象加入到作用域链中,但如何判断该元素是否“属于”一个表单对象,各浏览器的处理方式则不相同。 如以下代码:
<form action="." method="get">
 <div>
  <span onclick="alert(method);">click</span>
 </div>
</form>
<script>
document.method="document.method";
</script>
在各浏览器中,点击 SPAN 元素后弹出的信息如下:
IE Safari Opera get
Chrome Firefox document.method
可见: [list] [*]IE Safari Opera 将 FORM 对象加入到了[b]内联事件处理函数[/b]的作用域链中,是否加入 FORM 对象看起来是由这个元素是否是一个 FORM 的子孙级元素来决定的。因此在这些浏览器中,函数内的变量 'method' 最终得到的是 FORM 的 'method' 的值。 [/*][*]Chrome Firefox 没有将 FORM 对象加入到[b]内联事件处理函数[/b]的作用域链中,判断是否加入 FORM 对象是看该函数绑定的目标对象的 'form' 属性是否存在。从上文中的 WebKit 的源码中可以看到 Chrome 正是使用了 'this.form' 来判断,只有目标元素是一个 FORM 的子孙级元素并且该目标元素是一个表单元素时,'form' 属性才会存在。本例中的 SPAN 元素并不是表单元素,因此变量 'method' 最终得到的是 'document.method' 的值。 [/*][/list] 如果将以上代码中的 SPAN 元素更换为 INPUT 元素或其他表单元素,则在所有浏览器中的表现将一致。 [h2]3. 由于内联事件处理函数的这种特殊的作用域链而产生问题的实例[/h2] [h3]3.1. 在元素的内联事件处理函数中访问的变量意外的与该该函数作用域链中非全局对象的其他对象的属性重名时出现的问题[/h3] 当一个内联事件处理函数中访问的变量意外的与该函数作用域链中非全局对象(window)的其他对象的属性重名,将导致该变量的实际值不是预期值。 假设有以下代码:
<button onclick="onsearch()"> click here </button>
<script>
function onsearch(){
 alert("Click!");
}
</script>
作者本意为点击按钮即弹出“Click!”信息,但 WebKit 引擎浏览器的 HTMLElement 对象都有一个名为 onsearch 的事件监听器,这将导致上述代码在 Chrome Safari 中不能按照预期执行。本例中由于该监听器未定义(为 null),因此将报 “Uncaught TypeError: object is not a function” 的错误。 附:在上述代码中,追加以下代码确认 'onsearch' 的位置:
<script>
var o=document.getElementsByTagName("button")[0];
if("onsearch" in o)alert("当前对象有 onsearch 属性。");
if(o.hasOwnProperty("onsearch"))alert("onsearch 属性是当前对象私有。");
</script>
[h3]3.2. 在表单内的子孙级非表单元素的内联事件处理函数中试图调用表单的属性或方法时出现的问题[/h3] 假设有以下代码:
<form action="xxx" method="get">
 ...
 <a href="#" onclick="submit();">click</a>
</form>
作者本意为点击 A 元素后调用 FORM 的 'submit' 方法,但 Chrome Firefox 并未将 FORM 对象加入到该[b]内联事件处理函数[/b]的作用域链中,因此以上代码在 Chrome Firefox 中并不能正常运行。 [h1]解决方案[/h1] 1. 尽量不要使用[b]内联事件处理函数[/b],使用 DOM 标准的事件注册方式为该元素注册事件处理函数,如:
<button> click here </button>
<script>
function onsearch(){
 alert("Click!");
}
function bind($target,eventName,onEvent){
 $target.addEventListener?$target.addEventListener(eventName,onEvent,false):$target.attachEvent("on"+eventName,onEvent);
}
bind(document.getElementsByTagName("button")[0],"click",onsearch);
</script>
2. 必须使用内联事件处理函数时,要保证该函数内试图访问的变量是位于全局作用域内的,而不会因该函数独特的作用域链而引用到非预期的对象。最简单的办法是使用前缀,如 'my_onsearch'。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部