javascript插入视频类
目录
在上一篇中是利用jQuery来实现插入视频,那为什么不用js原生来写呢,这样就不加载jQuery的库了。顺便学习了javascript的面向对象实现类。
调用方式如下
/**
*先实例化对象,调用对应的实例的方法
*/
var v= new video('video');
//插入youku的视频
v.youku('rCWLA1bRkUE');
//可自定义宽度和高度
v.youku('rCWLA1bRkUE',100,100);
//tudou
//v.tudou('rCWLA1bRkUE');
//ku6
//v.ku6('XXX');
//youtube
//v.youtube('XXX');
video.js源代码
/**
*javascript 插入视频类
*Author:Arthur
*Blog:https://xbc.me
*/
function video(id){
this.id = id;
this.flash = '<object width="mywidth" height="myheight" type="application/x-shockwave-flash" data="myurl"><param name="quality" value="high"><param name="allowScriptAccess" value="always"><param name="flashvars" value="playMovie=true&isAutoPlay=true"></object>';
this.youku = function(code,width,height){
if(width == undefined){
width = 480;
}
if(height == undefined){
height =400;
}
var url ='http://player.youku.com/player.php/sid/myid/v.swf';
document.getElementById(this.id).innerHTML = this.flash.replace(/myurl/,url.replace(/myid/,code)).replace(/mywidth/,width).replace(/myheight/,height);
}
this.tudou = function(code,width,height){
if(width == undefined){
width = 480;
}
if(height == undefined){
height =400;
}
var url ='http://www.tudou.com/v/myid/v.swf';
document.getElementById(this.id).innerHTML = this.flash.replace(/myurl/,url.replace(/myid/,code)).replace(/mywidth/,width).replace(/myheight/,height);
}
this.ku6 = function(code,width,height){
if(width == undefined){
width = 480;
}
if(height == undefined){
height =400;
}
var url ='http://player.ku6.com/refer/myid/v.swf';
document.getElementById(this.id).innerHTML = this.flash.replace(/myurl/,url.replace(/myid/,code)).replace(/mywidth/,width).replace(/myheight/,height);
}
this.youtube = function(code,width,height){
if(width == undefined){
width = 480;
}
if(height == undefined){
height =400;
}
var url ='http://www.youtube.com/v/myid&hl=en_US&fs=1&autoplay=1';
document.getElementById(this.id).innerHTML = this.flash.replace(/myurl/,url.replace(/myid/,code)).replace(/mywidth/,width).replace(/myheight/,height);
}
}