代码之家  ›  专栏  ›  技术社区  ›  Martin Gordon

火狐3.0无限刷新循环

  •  2
  • Martin Gordon  · 技术社区  · 15 年前

    我在火狐3.0.x中的javascript出现了一个奇怪的问题。在火狐3.0.12中,一旦加载了列表主体,页面就会不断重新加载。火狐3.5、Safari 4和Chrome5(都在Mac上)都没有遇到过这个问题。

    编辑 :我创建了一个独立的示例,而不是从现有代码中提取这个示例。

    该问题与一个错误有关,该错误导致在FF 3.0中将location.hash设置为空字符串时重新加载页面。

    JS

    function welcomeIndexOnLoad() {
      $("#options a").live('click', function () {
        optionClicked($(this), "get_list_body.html");
        return false;
      });
    
      $(document).ready(function() {
        optionClicked(null, "get_list_body.html");
      });
    }
    
    function optionClicked(sender, URL) {
      queryString = "";
      if (sender != null) {
        queryString = $(sender).attr("rel");
      }
      $("#list_body").load(URL + "?" + queryString, function(resp, status, AJAXReq) {
        console.log(resp);
        console.log("" + status);
        location.hash = queryString;
      });
    }​
    

    测试HTML

    <html>
    <head>
      <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.js"></script>
      <script type="text/javascript" src="test.js"></script>
      <script>
        welcomeIndexOnLoad();
      </script>
    </head>
    
    <body>
    <div id="container">
      Outside of list body.
      <div id="list_body">
      </div>
    </div>
    </body>
    </html>
    

    获取\u list \u body.html

    <h3>
      <div id="options">
        <a href="#" rel="change_list">Change List</a>
      </div>
    <ul>
      <li>li</li>
    </ul>
    

    jquery行5252(xhr.send()调用)在页面重新加载后立即显示在控制台中:

    xhr.send( type === "POST" || type === "PUT" || type === "DELETE" ? s.data : null );
    
    2 回复  |  直到 15 年前
        1
  •  3
  •   Martin Gordon    15 年前

    经过更多的搜索,我发现了这个 blog post 其中提到,在firefox 3.0中将位置哈希设置为空字符串会导致页面刷新。

    将默认的querystring更改为“”,而不是空字符串,可以解决问题。

        2
  •  0
  •   Val    15 年前

    最有可能的是live函数,因为它有一个常量流,直到调用die()函数才会结束。试着用 die().live(...)

    虽然有时候看起来它刷新了,但情况可能并非如此,可能只是浏览器处理内存不好,而这个循环可能会耗尽它,称之为内存泄漏。

    另一个原因可能是您的Ajax页面多次加载。