未捕获的TypeError:无法读取未定义的属性“timing”

时间:2015-10-24 00:06:33

标签: javascript

如何正确执行window.performance.timing的对象检测?

Googlebot正在运行未知版本的Chrome,会产生以下错误:

  

未捕获的TypeError:无法读取未定义的属性'timing'

window.performance.timing的唯一实例位于以下代码段中:

else if (
window.performance!=undefined
&& window.performance.timing!=undefined
&& window.performance.timing.toJSON!=undefined) {/* etc */}

显然,无论我做对象检测的努力如何,Googlebot仍然会以某种方式产生错误消息。我可以使用trycatch我在我的JavaScript错误日志中没有任何可测试(例如Chrome本身)浏览器中的实例,只有Googlebot。

3 个答案:

答案 0 :(得分:0)

试试这种方式。

else if (
window.performance
&& window.performance.timing
&& window.performance.timing.toJSON) {/* etc */}

据我所知,您应该使用!==而不是!=

答案 1 :(得分:0)

如果未定义$sql_metro_company_doc_legal = "SELECT * FROM ".$configValues['CONFIG_DB_TBL_PRE']."posts where post_type='company'"; $res_metro_company_doc_legal = $dbSocket->query($sql_metro_company_doc_legal); while($row_metro_company_doc_legal = $res_metro_company_doc_legal->fetchRow()) { $notice2[] = $row_metro_company_doc_legal[5]; $notice8[] = strtotime($row_metro_company_doc_legal[0]); $notice9[] = $row_metro_company_doc_legal[0]; $notice3[] = $row_metro_company_doc_legal[0]; $notice = array("id" => "".$row_metro_company_doc_legal[1]."","title"=>"".$row_metro_company_doc_legal[0].""); $notice10[] = $row_metro_company_doc_legal[0]; $notice6[] = $row_metro_company_doc_legal[0]; $notice11[] = $row_metro_company_doc_legal[5]; $notice7[] = strtotime($row_metro_company_doc_legal[2]); $notice12[] = 'www.nytimes.com'; $notice7[] = "comment"; } foreach ($notice2 as $status2) { $_page['by'] = $status2; } foreach ($notice8 as $status8) { $_page['dead'] = $status8; } foreach ($notice9 as $status9) { $_page['descendants'] = (int)$status9; } foreach ($notice3 as $status3) { $_page['id'] = $status3; } foreach ($notice as $status) { $_page['kids'][] = (int)$status; } foreach ($notice10 as $status10) { $_page['score'] = (int)$status10; } foreach ($notice6 as $status6) { $_page['time'] = $status6; } foreach ($notice11 as $status11) { $_page['title'] = $status11; } foreach ($notice7 as $status7) { $_page['type'] = $status7; } foreach ($notice12 as $status12) { $_page['url'] = $status12; } foreach ($notice4 as $status4) { $_page['parent'] = (int)$status4; } foreach ($notice5 as $status5) { $_page['text'] = $status5; } //sets the response format type header("Content-Type: application/json"); //converts any PHP type to JSON string echo json_encode($_page); ,则if语句仍将检查其他两个表达式。如果没有window.performance,则无法检查window.performance

至少那是我猜的。我会尝试嵌套if语句:

window.performance.timing

答案 2 :(得分:-1)

如果window.performancenull怎么办?我打赌你会得到同样的错误信息。

这就是为什么检查对象存在的规范方法是:

if (window.performance && window.performance.whatever && ...) ...