javascript window.URL在函数中是未定义的

时间:2012-05-10 07:41:23

标签: javascript html5 video

我正在学习一些javascript,选择一个文件并用它来创建一个objectUrl,可以将其设置为src的{​​{1}}。我正在尝试这个<在html5 video上使用strong> chrome 版本18.0.1025.162,并在同一文件夹中使用包含javascript文件和媒体文件的本地html文件。

我可以使用输入元素选择视频文件,当我点击ubuntu lucid时,会执行javascript函数playVideo()。

play link

javascript文件是

<video id="vid" name='myvid' width="640" height="360" controls="controls">
       <source src="test.webm" type="video/webm" />
</video>
<br><a href="#" id="playlnk">Play </a> </li>
<br><input type="file" name="fileselect" id="fselect"> </input>

当我没有选择任何文件并点击播放列表时,默认视频播放和控制台日志显示$(document).ready(function(){ player=$('#vid').get(0); $('#playlink').click(function(){playVideo(player);}); }); function setVideoSrc(player,file){ console.log('winurl='+window.URL); var fileURL = window.URL.createObjectURL(file); player.src=fileURL; player.load(); return; } function playVideo(player) { var file=document.getElementById('fselect').files[0]; console.log('you chose:'+file); if (file==undefined){ console.log('no file chosen'); }else{ console.log('file='+file); setVideoSrc(player,file); } } - 正如预期的那样。

我选择了一个视频文件,然后点击了playlink 时发生错误。然后no file chosen方法调用playVideo(),其中控制台日志显示setVideoSrc() undefined`

为什么会发生这种情况?有人可以帮我纠正这个吗? 这是控制台日志输出

window.URL' is

3 个答案:

答案 0 :(得分:6)

在Chrome中使用window.webkitURL。

这适用于Chrome和FireFox

function setVideoSrc(player,file){
    var myURL = window.URL || window.webkitURL
    console.log('winurl='+myURL);
    var fileURL = myURL.createObjectURL(file);
    player.src=fileURL;
    player.load();
    return;
}

另见:

答案 1 :(得分:0)

尝试这种方式: -

var file = document.getElementById('fselect').files[0];
if(file){
  setVideoSrc(player,file); 
}

以下专栏适用于Chrome和Firefox: -

window.URL.createObjectURL(file);

请确保您正在测试上述浏览器。

请参阅此信息

答案 2 :(得分:-1)

你试过吗

 window.location.href = "URL";

而不是window.url?似乎url不支持功能。