HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavascriptModerate

Checking conditions before allowing a play button to become visible

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
allowingvisiblecheckingplaybuttonconditionsbeforebecome

Problem

I have this code to check a couple conditions before allowing a play button to become visible. Is there anything to do to consolidate this code?

```
var isNodeWebkit = (typeof process == "object"); // check if using node app
var downloadURL = 'https://www.microsoft.com/getsilverlight/get-started/install/'; // setup download URL variable
var isEncoding = false; // setup isEncoding variable

if (!Silverlight.isInstalled("1.0")) { // check if silverlight is installed
$('.playbtn').hide();
$('.videoposter').append('');
if (isNodeWebkit) {
$('.installbtn').on('click', function () { // open silverlight download externally
require('nw.gui').Shell.openExternal(downloadURL);
var win = require('nw.gui').Window.get();
win.close(); return false;
});
} else {
$('.installbtn').click(function () { // open silverlight download in new tab
window.open(downloadURL, '_blank');
});
$(window).focus(function () {
if (Silverlight.isInstalled("1.0")) {
$('.installbtn').remove();
$('.playbtn').show(); checkEncodingFunc();
} else if (!Silverlight.isInstalled("1.0")) {
$('.installbtn').remove(); $('.videoposter').append('');
$('.playbtn').hide(); $('.encodingbtn').hide();
}; ;
});
};
} else if (Silverlight.isInstalled("1.0")) {
$(window).focus(function () { // after installing show play button
if (!Silverlight.isInstalled("1.0")) {
$('.playbtn').hide(); $('.installbtn').remove(); $('.encodingbtn').remove();
$('.videoposter').append('');
$('.installbtn').click(function () { // open silverlight download in new tab
window.open(downloadURL, '_blank');
});
} else {
$('.installbtn').remove();
$('.playbtn').show();
};
if (Silverlight.isInstalled("1.0") && isEncoding

Solution

Ok first of all.. Silverlight Rant.

Silverlight was a flop far less successful than Microsoft expected it to be. HTML5 + WebGL, on the other hand, is the future of handling anything that anyone ever thought Silverlight might do. If you want to maintain highly supported, future-proof practices, start doing some WebGL research. It's INCREDIBLE. Silverlight is another one of Microsoft's attempts to avoid standardization and control the web with their own tech. They've failed every single time so far.

Lesson: Avoid Microsoft's proprietary stuff when possible.

Now, for a Review on Practices..

As far as your code formatting and practices go, I'm happy with what I see. There are a few improvements you can make to make your code a bit simpler:

You use if(!Silverlight.isInstalled("1.0")) // do stuff all over the place. Try this instead:

var silverlightCheck = Silverlight.isInstalled("1.0");
if (silverlightcheck) // do stuff


I saw these on the same line: $('.playbtn').show(); checkEncodingFunc(); Nu uh. Nope. Use:

$('.playbtn').show(); 
checkEncodingFunc();


And one more simplification:

You can make selectors that you often repeat, such as $('.installbtn') into variables.

var installButton = $('.installbtn'); //


That way, you type that selector a little quicker via the variable.

I've touched on some simplification. Someone might review this to contribute improvement to the structure, but this is not bad code. +1 for you!

Code Snippets

var silverlightCheck = Silverlight.isInstalled("1.0");
if (silverlightcheck) // do stuff
$('.playbtn').show(); 
checkEncodingFunc();
var installButton = $('.installbtn'); //

Context

StackExchange Code Review Q#57739, answer score: 14

Revisions (0)

No revisions yet.