简体   繁体   English

如何使用PageSpeed获得移动和桌面得分

[英]How to get both mobile and desktop score with PageSpeed

I'm trying to get page speed score for both: desktop and mobile and then pass them to #pgscore && #pgscorem inputs. 我想同时获得台式机和移动设备的页面速度得分,然后将它们传递给#pgscore && #pgscorem输入。

Problem is that at the end of script I get always var device == 'mobile'. 问题是在脚本结束时,我总是得到var device =='mobile'。 It looks like it skips for loop. 看起来它跳过了循环。 Any idea how could I fix it? 知道我该如何解决吗?

for (var r = 0; r < 2; r++) {
var API_KEY = 'mykey';
var device = '';

switch (r) {
    case 0: device='desktop'; break; 
    case 1: device='mobile'; break;
};  
alert(device);
var URL_TO_GET_RESULTS_FOR = 'http://www.stackoverflow.com' + '&strategy=' + device;

var API_URL = 'https://www.googleapis.com/pagespeedonline/v1/runPagespeed?';
var CHART_API_URL = 'http://chart.apis.google.com/chart?';


var callbacks = {}


function runPagespeed() {
  var s = document.createElement('script');
  s.type = 'text/javascript';
  s.async = true;
  var query = [
    'url=' + URL_TO_GET_RESULTS_FOR,
    'callback=runPagespeedCallbacks',
    'key=' + API_KEY,
  ].join('&');
  s.src = API_URL + query;
  document.head.insertBefore(s, null);
}


function runPagespeedCallbacks(result) {
  if (result.error) {
    var errors = result.error.errors;
    for (var i = 0, len = errors.length; i < len; ++i) {
      if (errors[i].reason == 'badRequest' && API_KEY == 'yourAPIKey') {
        alert('Please specify your Google API key in the API_KEY variable.');
      } else {

        alert(errors[i].message);
      }
    }
    return;
  }

  for (var fn in callbacks) {
    var f = callbacks[fn];
    if (typeof f == 'function') {
      callbacks[fn](result);
    }
  }
}

setTimeout(runPagespeed, 0);


callbacks.displayPageSpeedScore = function(result) {
  var score = result.score;

Function countinues down there. 功能就在那儿。 Problem is here... Why I can't get variable device == 'desktop' first and then in second for loop 'mobile'? 问题在这里...为什么我不能先获取变量device =='desktop',然后再获取for loop'mobile'? I get always 'mobile'. 我总是“移动”。

switch (device) {
        case 'desktop': $('#pgscore').val(score); break; 
        case 'mobile': $('#pgscorem').val(score); break;
    };


 };



};

Since I need only score, I'm using php 因为我只需要分数,所以我正在使用php

$jsonurl="https://www.googleapis.com/pagespeedonline/v1/runPagespeed?url=$url&locale=pl_pl&$key"; 
  $json = file_get_contents($jsonurl);  
  $json_output = json_decode($json, true); 
  /* print_r(array_values($json_output));*/
  echo $json_output['score'];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用谷歌图表在Google PageSpeed上获得100分? - how to score 100 on google PageSpeed with google charts? 桌面和移动设备的jQuery事件 - jQuery events for both desktop and mobile 如何在桌面和移动网络上使用通用的javascript文件? - How to use the common javascript files for both desktop and mobile web? 如何获取JavaScript在桌面而不是移动设备上运行功能 - How to get javascript to run a function on desktop but not on mobile 如何为Ajax加载的页面集成Google Analytics(分析)(PageSpeed和Pageview两者)? - How to Integrate Google Analytics (PageSpeed and Pageview both) for Ajax Loaded Pages? 如何从此代码自定义移动和桌面显示的警报框? - How can I customised alert boxes for both mobile and desktop display from this code? 如何检测移动设备或桌面设备而不被 chrome 开发工具“欺骗” - How to detect mobile or desktop and not get "cheated" by chrome dev tools 为什么我“消除了渲染阻止资源”后却得到较低的PageSpeed分数? - Why do I get a lower PageSpeed score after I “eliminate render-blocking resources”? Google PageSpeed得分 - 1渲染阻止CSS文件 - Google PageSpeed score - 1 Render Blocking CSS file 任何仅具有一个同时支持桌面和移动设备的API的Javascript框架? - Any Javascript framework with just one API that supports both Desktop and Mobile?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM