私はできる限りこれを説明しようとしています。これが意味をなさない場合はお詫び申し上げます。object.prototype breaking javascript
「Object.prototype」は、javascriptを壊しているようですが、これを修正する方法はわかりません。私はYouTubeのAPIを使用していますが、10本の動画(XMLファイル)をループして繰り返すスクリプトを作成しました。
あなたは、コードが破壊されていることを私は「Object.prototype.getKey =関数(値){...」
を持って気づくのjavascriptの一番下に私のコードを見れば私のページのjavascriptの他の部分。それを削除するとYouTubeプレーヤーが次の動画にループしなくなります。だから私はそれをすることはできません。この周りとにかく
Uncaught TypeError: X[g].exec is not a function
at fb.tokenize (jquery-latest.min.js:2)
at Function.fb [as find] (jquery-latest.min.js:2)
at m.fn.init.find (jquery-latest.min.js:2)
at HTMLDivElement.<anonymous> (photoAds-slider.js:37)
at Function.each (jquery-latest.min.js:2)
at m.fn.init.each (jquery-latest.min.js:2)
at m.fn.init.d.fn.flexslider (photoAds-slider.js:37)
at HTMLDocument.<anonymous> (screen.php:44)
at j (jquery-latest.min.js:2)
at Object.fireWith [as resolveWith] (jquery-latest.min.js:2)
:ここ
はエラーですか?PHP
// ========================================
// Load the xml files for youtube player (YouTube IDs)
// ========================================
$xml1=simplexml_load_file("youtube/1.xml");
$youtube1 = $xml1->video;
$xml2=simplexml_load_file("youtube/2.xml");
$youtube2 = $xml2->video;
$xml3=simplexml_load_file("youtube/3.xml");
$youtube3 = $xml3->video;
$xml4=simplexml_load_file("youtube/4.xml");
$youtube4 = $xml4->video;
$xml5=simplexml_load_file("youtube/5.xml");
$youtube5 = $xml5->video;
$xml6=simplexml_load_file("youtube/6.xml");
$youtube6 = $xml6->video;
$xml7=simplexml_load_file("youtube/7.xml");
$youtube7 = $xml7->video;
$xml8=simplexml_load_file("youtube/8.xml");
$youtube8 = $xml8->video;
$xml9=simplexml_load_file("youtube/9.xml");
$youtube9 = $xml9->video;
$xml10=simplexml_load_file("youtube/10.xml");
$youtube10 = $xml10->video;
$volume = '100';
$quality = '720';
$videos = array("$youtube1", "$youtube2", "$youtube3", "$youtube4", "$youtube5", "$youtube6", "$youtube7", "$youtube8", "$youtube9", "$youtube10",);
// print_r(array_values($videos));
JAVSCRIPT
playlistids = <?php echo json_encode($videos); ?>;
var playing = playlistids[0];
var ytplayer;
function onYouTubeIframeAPIReady() {
ytplayer = new YT.Player('ytapiplayer', {
width: '100%',
height: '100%',
videoId: playing,
events: {
'onStateChange': onytplayerStateChange,
'onReady': onPlayerReady
},
playerVars: {
'controls': 0,
// 'cc_load_policy': 1,
'rel' : 0,
'showinfo' : 0,
}
});
}
function onPlayerReady(event) {
event.target.setPlaybackQuality(<?php echo $quality; ?>)
event.target.setVolume(<?php echo $volume; ?>)
// alert('Start playing entry #0 (preloaded).')
event.target.playVideo()
// Fade out music player
audio_fade_out();
}
function play(ytid) {
if (ytplayer) {
playing = ytid
// alert('On to entry #'+ nextentrykey +', playing set to: ' + playing)
ytplayer.loadVideoById(ytid, 0, <?php echo $quality; ?>);
}
}
function onytplayerStateChange(event) {
//alert('NEW STATE: ' + event.data)
if (event.data == 0) {
setTimeout(youtubeTimer, 4200);
// Fade in music player
audio_fade_in();
// alert('Since the new player state is '+ event.data +', the video has ended. Getting next key after playing ' + playing + '.');
nextentrykey = parseInt(playlistids.getKey(playing))+1
if (nextentrykey >= playlistids.length) {
nextentrykey = 0
}
document.getElementById("ytapiplayer").style.visibility = "hidden";
}
}
function youtubeTimer() {
play(playlistids[nextentrykey]);
document.getElementById("ytapiplayer").style.visibility = "visible";
// Fade out music player
audio_fade_out();
}
Object.prototype.getKey = function(value){
for(var key in this){
if(this[key] == value){
return key;
}
}
return -1;
};
} else {
// alert('YouTube is OFF')
document.getElementById("ytapiplayer").style.visibility = "hidden";
}
'getKey'を通常の関数にし、代わりに' getKey(playlistids、playing) 'を呼び出すのはどうですか?組み込みのプロトタイプを拡張するのは悪い習慣です。最初にプロトタイプを拡張したのはなぜですか? –
「私のjavascriptを壊しているようだ」とはどういう意味ですか?ブラウザは例外をスローしますか? 'nextentrykey = parseInt(playlistids.getKey(playing))+ 1'を使用するとエラーが発生しますか?どうした? – Nope
ご迷惑をおかけして申し訳ありません。それは私に戻ってきているエラーです。 https://www.pastiebin.com/58a7276751a6a –