0

I am not experienced in Javascript, I have the following script to play video files on Andriod phone, and it works fine.

 <script type="text/javascript">
        function PlayMyVideo(arg) {
            var myVideo = document.getElementById([arg]);
            myVideo.play();
        }
    </script>
<video id="what" src="what.mp4" poster="" />
<input type="button" onclick="PlayMyVideo('what')" value="Play" />

I am trying to write the tag on the fly:

  <script type="text/javascript">
        function PlayVideo() {
            new_video = document.createElement('video');
            new_video.setAttribute('scr', 'what.mp4'); 
            new_video.play(); 
         }
    </script>
<input type="button" onclick="PlayVideo()" value="Play2" />

Nothing happen, would appreciate your suggestions. Thanks in advance

0

3 Answers 3

4
new_video.setAttribute('scr', 'what.mp4');

'scr' is misspelled. It should be 'src'.

and also you should wait for the movie to load before play

Sign up to request clarification or add additional context in comments.

Comments

2

Well you're not appending the newly created tag to anything, so it can't play because it's in "memory"/"void", not on the screen.

<div id='plc'>&nbsp;</div>

<script type='text/javascript'>
function PlayVideo() {
new_video = document.createElement('video');
document.getElementById('plc').appendChild(new_video);
new_video.setAttribute('scr', 'what.mp4');
new_video.play();
}

4 Comments

Thanks, it created the video element; however the video did not play.
that's strange, maybe changing new_video.play() to window.onload = function() { new_video.play(); } would help
Thanks Slawek, it did not work at first, then I changed ' to " for the scr and the original script you gave me first worked great but on iPad not on Android.
Just as a follow up, this tag works well on Android:<video src="what.mp4" onclick="this.play();"/>. Since I have a number of video clips, I thought it will be more efficient to make it as javascript were I can use variable for the clip name. Thanks again
1

you are creating the video element, you need to add it to the DOM before it will be visible, more info here: http://www.javascriptkit.com/javatutors/dom2.shtml

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.