【问题标题】:Script is causing link to call server twice脚本导致链接调用服务器两次
【发布时间】:2012-03-02 17:28:05
【问题描述】:

使用this gist 防止链接在网站保存为主屏幕网络应用程序时启动浏览器。

脚本在一定程度上是有效的。问题是由于某种原因它两次调用目标页面。这导致 Jquery Mobile 在每次页面加载时都显示 error loading page,尽管已成功加载页面。

这是一个示例链接:

<a href="/bookings/3/features" data-ajax="false">Link Example</a>

这是来自服务器的日志:

 Started GET "/bookings/3/features" for 10.104.4.210 at 2012-03-02 17:13:25 +0000
Processing by FeaturesController#index as HTML
  Parameters: {"booking_id"=>"3"}
  Booking Load (0.3ms)  SELECT `bookings`.* FROM `bookings` WHERE `bookings`.`id` = 3 LIMIT 1
  Car Load (0.2ms)  SELECT `cars`.* FROM `cars` WHERE `cars`.`id` = 2 LIMIT 1
  CarModel Load (0.2ms)  SELECT `car_models`.* FROM `car_models` WHERE `car_models`.`id` = 1 LIMIT 1
  Manufacturer Load (0.2ms)  SELECT `manufacturers`.* FROM `manufacturers` WHERE `manufacturers`.`id` = 1 LIMIT 1
  Feature Load (0.3ms)  SELECT `features`.* FROM `features` WHERE `features`.`car_model_id` = 1
   (0.2ms)  SELECT COUNT(*) FROM `car_colours` WHERE `car_colours`.`car_model_id` = 1
  Rendered features/index.html.erb within layouts/frontEnd (5.2ms)
  Rendered shared/_footer.html.erb (1.1ms)
Completed 200 OK in 62ms (Views: 56.8ms | ActiveRecord: 1.4ms)


Started GET "/bookings/3/features" for 10.104.4.210 at 2012-03-02 17:13:25 +0000
Processing by FeaturesController#index as HTML
  Parameters: {"booking_id"=>"3"}
  Booking Load (0.4ms)  SELECT `bookings`.* FROM `bookings` WHERE `bookings`.`id` = 3 LIMIT 1
  Car Load (0.2ms)  SELECT `cars`.* FROM `cars` WHERE `cars`.`id` = 2 LIMIT 1
  CarModel Load (0.2ms)  SELECT `car_models`.* FROM `car_models` WHERE `car_models`.`id` = 1 LIMIT 1
  Manufacturer Load (0.2ms)  SELECT `manufacturers`.* FROM `manufacturers` WHERE `manufacturers`.`id` = 1 LIMIT 1
  Feature Load (0.3ms)  SELECT `features`.* FROM `features` WHERE `features`.`car_model_id` = 1
   (0.2ms)  SELECT COUNT(*) FROM `car_colours` WHERE `car_colours`.`car_model_id` = 1
  Rendered features/index.html.erb within layouts/frontEnd (4.5ms)
  Rendered shared/_footer.html.erb (1.1ms)
Completed 200 OK in 26ms (Views: 20.8ms | ActiveRecord: 1.3ms)

【问题讨论】:

    标签: javascript ruby-on-rails web-applications jquery-mobile mobile-safari


    【解决方案1】:

    问题在于 jQuery Mobile 尝试通过执行 ajax 而不是常规页面加载来优化页面加载并最小化请求。这会导致 jQuery mobile 发出第一个请求,当 jQuery 等待返回时,“stay_standalone”脚本会触发并在常规 http 页面加载中加载页面。

    您可以在这两个链接中看到与此功能相关的 jQuery Mobile 代码:first in the init,然后是checking for ajax flag and finally making a request。这也是in jQuery Mobile's global config docs

    我在独立模式下搜索了一些关于 jQuery Mobile 的信息,但没有找到任何具体的内容。这个问题有两种解决方法:

    1. 从您的站点/网络应用程序中删除要点。这可能是一个足够的解决方案,因为 jQuery Mobile 已经为您完成了 ajax - 并且链接不会在 Safari 中打开。此外,这可能会更快,请求更少。
    2. 如果这不能按预期工作,您可以禁用 jQuery Mobile 中的 ajaxEnabled 功能,如他们的文档中所述:

      $(document).bind("mobileinit", function(){ ajaxEnabled: 假 });

    第二个选项不是最佳的,但可能是一个很好的解决方案,直到您弄清楚 jQuery Mobile 如何阻止页面在 Mobile Safari 中加载。

    【讨论】:

    • 我需要脚本处理一个链接。如果我这样做,我该死,如果我不这样做,我该死!
    【解决方案2】:

    对此并不完全确定,但您是否应该确保您的脚本返回 false?

    【讨论】:

      猜你喜欢
      • 2016-11-02
      • 2015-04-15
      • 1970-01-01
      • 2017-09-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多