user11301026
user11301026

Reputation:

Trying to set the src attribute of a video using JavaScript, getting TypeError: videofile is null

I have a video and some JavaScript code. The code says

var videoIDRAW = getUrlVars()["watch"];
var videoID = videoIDRAW.toString();
var videoIDFILE = videoID.concat(".mp4");
var videofile = document.getElementById("vid");

videofile.setAttribute("src", videofile)

videofile is

<video width="912" height="513" controls >
<source id="vid" src="" type="video/mp4">

</video>

why do i get the TypeError: videofile is null?

EDIT: modified videofile.setAttribute("src", videofile.toString()), same error

Upvotes: 0

Views: 527

Answers (1)

Seblor
Seblor

Reputation: 7136

toString() is not a magical tool. It should have been called by default anyway I think.

As Aaros pointed out, the issue lies in what your algorithm tries to do. You are trying to set a DOM element as the value of one of its own attributes. The src attribute of a video needs to be the video's URL (or blob, but that's more complicated).

Didn't you mean this ?

videofile.setAttribute("src", videoIDFILE); // Or videofile.src = videoIDFILE

Edit: Don't forget to execute your script after the body. Either by moving the script tag to the bottom of the body (but still inside), or you can use a listener like you can find there : How to execute a function when page has fully loaded?

Upvotes: 1

Related Questions