代码之家  ›  专栏  ›  技术社区  ›  iangraham

导致ie“null”为空或不是对象错误的javascript重定向

  •  0
  • iangraham  · 技术社区  · 15 年前

    我正在尝试A/B测试我们的产品放置视图。下面是我使用的javascript代码:

      var url = window.location.href,
          defaultType = 'Gallery',
          otherType = 'List';
      if(Cookie.read('SEARCHVIEW') == otherType && Cookie.read('viewTypeChanged') != otherType){
          if(url.contains('v='+otherType)){
            url = url.replace('v='+otherType, 'v='+defaultType);
          }else if(url.contains('?')){
            url = url + '&v='+defaultType;
          }else{
            url = url + '?v='+defaultType;
          }
    
          window.location = url;
        }
    

    在页面重新定向后 'null' is null or not an object Object does not support this property or method 错误。

    这发生在IE 7和8中

    我怎么修这个?

    2 回复  |  直到 12 年前
        1
  •  0
  •   Bleeding Fingers    12 年前

    我能解决这个问题。为了以后的参考,我会发布一些代码和解释。 有问题的代码如下:

    <script type="text/javascript">
    
     window.addEvent('domready', function(){
    
    
      $('v_toggle').addEvents({
       'mouseenter': function(e){
       $('vertical_slide_container').show();
    
      },
       'mouseleave': function(e){
       $('vertical_slide_container').hide();
    
      }
      });
     });
     </script>
    
    </head>
    
    <body>
    
    
    <div class="mboxDefault">/div>
    <script type="text/javascript">mboxCreate('header',etc...);</script>
    

    我的公司使用Omniture进行A/B测试,我的代码在 mboxCreate() 功能。您会注意到上面的代码添加了 domready 将悬停事件添加到导航AION的事件(偶然低于我的 MBOXCRATE() 代码)。发生的是我的代码正在重定向,这阻止了页面的加载,同时也表明DOM已经就绪。所以当我调用重定向时,dom就绪代码运行,并试图在不存在的dom元素上添加悬停事件。

    因此 'null' is null or not an object' 以及 object does not support this method or property 错误。

    解决这个问题的办法是把所有 多米德 重定向前的事件,因此我的无错误代码如下所示:

      var url = window.location.href,
          defaultType = 'Gallery',
          otherType = 'List';
      if(Cookie.read('SEARCHVIEW') == otherType && Cookie.read('viewTypeChanged') != otherType){
          if(url.contains('v='+otherType)){
            url = url.replace('v='+otherType, 'v='+defaultType);
          }else if(url.contains('?')){
            url = url + '&v='+defaultType;
          }else{
            url = url + '?v='+defaultType;
          }
          window.removeEvents('domready');
          window.location = url;
        }
    
        2
  •  -2
  •   Sushil Jaiswar    15 年前

    Href不是window的属性,它可以与anchor()对象一起使用,请尝试只使用window.location,这样应该可以工作。