javascript window.URL 在函数中未定义

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10529476/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 10:11:44  来源:igfitidea点击:

javascript window.URL is undefined in function

javascripthtmlvideo

提问by damon

I was learning some javascript,to select a file and use it to create an objectUrl which can be set as the srcof an html5 video.I am trying this out in chromeversion 18.0.1025.162 on ubuntu lucid, and using a local html file with a javascript file and media files in the same folder.

我是学一些javascript,选择一个文件,并使用它来创建可被设置为一个的ObjectURLsrc一个的html5 video。我是在想这一点镀铬版本18.0.1025.162上ubuntu lucid,并使用本地HTML文件的JavaScript文件和媒体同一文件夹中的文件。

I can select a video file using the input element and when I click on a play link,the javascript function playVideo() is executed.

我可以使用 input 元素选择一个视频文件,当我单击 a 时play link,将执行 javascript 函数 playVideo()。

<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>

javascript file is

javascript文件是

$(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);
     }     
}

When I don't select any file and click on the playlink ,the default video plays and console log says no file chosen-as expected.

当我没有选择任何文件并单击播放链接时,默认视频播放并且控制台日志显示no file chosen-正如预期的那样。

The error occurs when I select a video file and then click on the playlink. Then the playVideo()method calls setVideoSrc()in which the console log says window.URL' isundefined`

我选择一个视频文件然后单击播放链接时会发生错误。然后playVideo()调用setVideoSrc()控制台日志显示window.URL' is未定义的方法

Why does this happen?Can someone help me correct this? Here is the console log output

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

you chose:[object File] //from playVideo() function
file=[object File]   //from playVideo() function
winurl=undefined   //setVideoSrc() function
Uncaught TypeError: Cannot call method 'createObjectURL' of undefined 

采纳答案by Maurice

Use window.webkitURL in Chrome.

在 Chrome 中使用 window.webkitURL。

This whould work in both Chrome and FireFox

这适用于 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;
}

See also:

也可以看看:

回答by Siva Charan

Try this way:-

试试这个方法:-

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

Below line works on Chrome and Firefox:-

以下行适用于 Chrome 和 Firefox:-

window.URL.createObjectURL(file);

Make sure, you are testing on mentioned browsers.

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

Refer to this information

参考这个信息

回答by Johnny_D

Have you tried

你有没有尝试过

 window.location.href = "URL";

instead of window.url? Seems url isn't supported feature.

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