/    Sign up×
Community /Pin to ProfileBookmark

index.js:29 Uncaught TypeError: Cannot read property ‘length’ of null

Trying to make a website where a random webm is loaded when the page is entered and/or the video is clicked on but nothing seems to be loading. Here’s the javascript I was given;

[CODE]window.onload = function () {

// A list of your video URLs in a folder called vid
var myVideoList = [];

// Function to load a new video
var loadNewVideo = function () {
// Randomly select a video URL from myVideoList
var selectedVideo = myVideoList[Math.floor(Math.random() * myVideoList.length – 1) + 1];

// Set the source tag of the video and start it playing
var myVideo = document.getElementById(“my-video”);
var myVideoSource = document.getElementById(“my-video-source”);
myVideoSource.src = selectedVideo;
myVideo.load();
myVideo.play();
}

// Get list of *.webm files from folder
$.ajax({
method: “GET”,
url: “/vid”,
success: function(data) {
var files = data.match(/title=”(.*)”/g);

// add video paths to myVideoList
for (var i = 0; i < files.length; i++) {
if (files[i].indexOf(“.webm”) > -1) {
var temp = files[i].split(‘title=”‘);
temp = temp[1].replace(“””, “”);
myVideoList.push(“/vid/” + temp);
}
}

// load first video
loadNewVideo();

// Video click event
$(“#my-video”).click(function () {
loadNewVideo();
});
},
error: function () {
alert(“Error loading files”);
}
});

}[/CODE]

Any ideas? My browser console says the line [B]for (var i = 0; i < files.length; i++) {[/B] is the one giving trouble.

Thanks

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@rootJul 19.2016 — This is a JavaScript Framework question, better as in the right forum.

From a scripting stance, I don't see data declared anywhere that is populated by a function call passing data to it...
×

Success!

Help @wavey spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...