我正在使用在论坛上找到的脚本通过JavaScript滑块绑定(bind)到Vimeo API,但出现错误:
语法错误:JSON.parse:意外字符
它说此行上存在错误:
var data = JSON.parse(e.data);
这是整个脚本:
(function () {
var $=jQuery;
var f = $('iframe');
var url = f.attr('src').split('?')[0]; <?php //HACK! had to hard code the protocol in here or postMethod shows error: Uncaught SyntaxError: An invalid or illegal string was specified. ?>
//var status = $('.status');
// Listen for messages from the player
if (window.addEventListener){
window.addEventListener('message', onMessageReceived, false);
} else {
window.attachEvent('onmessage', onMessageReceived, false);
}
// Handle messages received from the player
function onMessageReceived(e) {
var data = JSON.parse(e.data);
switch (data.event) {
case 'ready':
onReady();
break;
case 'playProgress':
onPlayProgress(data.data);
break;
case 'pause':
onPause();
break;
case 'finish':
onFinish();
break;
}
}
// Call the API when a button is pressed
$('button').on('click', function() {
post($(this).text().toLowerCase());
});
// Helper function for sending a message to the player
function post(action, value) {
var data = { method: action };
if (value) {
data.value = value;
}
$('iframe')[0].contentWindow.postMessage(JSON.stringify(data), url);
}
function onReady() {
post('addEventListener', 'pause');
post('addEventListener', 'finish');
post('addEventListener', 'playProgress');
}
function onPause() {
console.log("vimeo paused");
}
function onFinish() {
playing = setInterval(function() {slide('left')}, 6000);
console.log("vimeo finish");
slide('left');
}
function onPlayProgress(data) {
clearInterval(playing);
console.log("vimeo play progress");
}
})();
谁有想法?谢谢!
最佳答案
问题是此代码:
var f = $('iframe');
如果您的网页上还有其他iframe,则代码将针对这些iframe。在我的情况下,可能是一个点赞/分享按钮。您必须使用以下内容填充f变量:
var f = $('。vimeoplayer');
即使这样,我仍然遇到问题。我认为如果不使用其迷你库“froogaloop”,使用普通的javascript/jquery与vimeo播放器进行交互是不可行的。我开始使用froogaloop,它解决了我所有的问题。 Froogaloop只是几行代码...完全值得。
http://developer.vimeo.com/player/js-api
关于javascript - Vimeo滑块脚本-SyntaxError : JSON.parse: unexpected character,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20081431/