我正在使用这一块代码将flowplayer放在页面上,它几乎使得flowplayer变得通用:它几乎适用于所有PC浏览器以及iphone,ipads,android设备......
<script>
$(document).ready(function() {
if (navigator.userAgent.match(/Android/i)
|| navigator.userAgent.match(/webOS/i)
|| navigator.userAgent.match(/iPhone/i)
|| navigator.userAgent.match(/iPad/i)
|| navigator.userAgent.match(/iPod/i)
|| navigator.userAgent.match(/BlackBerry/i)
) {
$f("player", "/flowplayer-3.2.16.swf", {
clip: {
provider: "rtmp",
url: "streamname",
ipadUrl: 'http://wowza.example:1935/live/_definst_/streamname/playlist.m3u8',
bufferLength: 2,
live: true,
},
plugins: {
rtmp: {
url: "/flowplayer.rtmp-3.2.12.swf",
netConnectionUrl: "rtmp://wowza.example:1935/live"
}
}
}).ipad({ simulateiDevice: true, controls: false });
}
else {
$f("player", "/flowplayer-3.2.16.swf", {
clip: {
provider: "rtmp",
url: "streamname",
ipadUrl: 'http://wowza.example:1935/live/_definst_/streamname/playlist.m3u8',
bufferLength: 2,
live: true,
},
plugins: {
rtmp: {
url: "/flowplayer.rtmp-3.2.12.swf",
netConnectionUrl: "rtmp://wowza.example:1935/live"
}
}
}).ipad();
}
});
</script>
我的问题是关于干净的代码。因为现在代码是有效的,但在我看来,我正在重复整个事情,if
和else
之间唯一不同的是最后一行:
}).ipad({ simulateiDevice: true, controls: false });
VS
}).ipad();
有没有办法在if
函数中移动ipad()
子句?不是jQuery或javascript开发人员,我一直在尝试这样做,但我无处可去......
答案 0 :(得分:0)
最简单的方法是为选项值simulateiDevice
和controls
创建变量。然后使用if语句设置它们的值。
示例强>
<script>
$(document).ready(function() {
var simulateiDevice,
controls;
if (navigator.userAgent.match(/Android/i) ||
navigator.userAgent.match(/webOS/i) ||
navigator.userAgent.match(/iPhone/i) ||
navigator.userAgent.match(/iPad/i) ||
navigator.userAgent.match(/iPod/i) ||
navigator.userAgent.match(/BlackBerry/i)) {
simulateiDevice = false;
controls = true;
} else {
simulateiDevice = true;
controls = false;
}
$f("player", "/flowplayer-3.2.16.swf", {
clip: {
provider: "rtmp",
url: "streamname",
ipadUrl: 'http://wowza.example:1935/live/_definst_/streamname/playlist.m3u8',
bufferLength: 2,
live: true,
},
plugins: {
rtmp: {
url: "/flowplayer.rtmp-3.2.12.swf",
netConnectionUrl: "rtmp://wowza.example:1935/live"
}
}
}).ipad({
simulateiDevice: simulateiDevice,
controls: controls
});
});
< /script>