我正在使用谷歌地图在我的网站上放置Javascrip库,以检索我的商店评论&评分,我在localhost中测试过,一切正常,但是当我在wordpress网站上使用代码时,我收到一条错误,说 getRelevantGoogleReviews不是函数我甚至试图为我的setTimeout设置3秒网站来获取评论,但它不起作用。知道为什么吗?
<!doctype html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
</head>
<body>
<ul class="reviews__content" id="reviews__content">
</ul>
<div id="service-helper"></div>
<script async defer src="https://maps.googleapis.com/maps/api/js?key=MY_API_KEY&libraries=places&callback=getRelevantGoogleReviews">
</script>
<script type="text/javascript">
window.getRelevantGoogleReviews = function() {
var service = new google.maps.places.PlacesService($('#service-helper').get(0)); // note that it removes the content inside div with tag '#service-helper'
service.getDetails({
placeId: 'ChIJ0YnbDpIUrjsRUiJwpVPcmDk' // get a placeId using https://developers.google.com/places/web-service/place-id
}, function(place, status) {
if (status === google.maps.places.PlacesServiceStatus.OK) {
var resultcontent = '';
for (i = 0; i < place.reviews.length; ++i) {
if (place.reviews[i].rating >= 4) {
//window.alert('Name:' + place.name + '. ID: ' + place.place_id + '. address: ' + place.formatted_address);
resultcontent += '<li class="reviews__item">'
resultcontent += '<div class="reviews__review-er">' + place.reviews[i].author_name + '</div>';
var reviewDate = new Date(place.reviews[i].time * 1000);
resultcontent += '<div class="reviews__review-er"> <p>Rating: ' + place.reviews[i].rating; + ' </p></div>';
var reviewDateMM = reviewDate.getMonth() + 1;
var reviewDateFormatted = reviewDate.getDate() + '/' + reviewDateMM + '/' + reviewDate.getFullYear();
resultcontent += '<div class="reviews__review-date">' + reviewDateFormatted + '</div>';
resultcontent += '<div class="reviews__review-rating reviews__review-rating--' + place.reviews[i].rating + '"></div>';
if (!!place.reviews[i].text) {
resultcontent += '<div class="reviews__review-comment">' + place.reviews[i].text + '</div>';
resultcontent += '<div class="reviews__review-comment"> <a target="_blank" href="'+place.reviews[i].author_url+'">click here</a></div>';
}
resultcontent += '</li>'
}
}
$('#reviews__content').append("<b>Corporate Office</b><br><br>" + resultcontent);
}
});
}
</script>
</body>
</html>
答案 0 :(得分:0)
看起来JS解释器似乎没有足够的时间来声明这个功能。以下是您可以采取的措施:
交换谷歌库和您的JS代码,以便您的代码在lib上方声明。
<script>//your code</script>
<script async defer src="...google..lib" />
这是您的代码JSfiddle