How to Convert your javascript file to a Windows .exe or .dll

Nowadays an average web developer will need to know a broad matrix of skills in order to do his/her development job. HTML, CSS, JavaScript, AJAX, XML, some server side language, some linux/unix/redhat command skills, some apache, some database skills, standards, accessibility, SEO, etc…, the list goes on. Parts of the list are also multiplied by (or raised to the power of at least 10) number of browsers you want to support.
What is Javascript can be used for?

  • create rich and powerful web applications (the kind that runs inside the browser)
  • write server-side code such as ASP scripts or for example code that is run using Rhino (A JavaScript engine written in Java)
  • create rich media applications (Flash, Flex) using ActionScript which is based on ECMAScript, which is JavaScript
  • write scripts that automate administrative tasks on your Windows desktop, using Windows Scripting Host
  • write extensions/plugins for a plethora of desktop application such as Firefox or Dreamweaver
  • create web applications that store information off-line on user’s desktop, using Google Gears
  • create Yahoo!, or Mac, or dunno-what-type-of widgets
  • create Windows apps (those that end in .exe) and libraries (.dll)

Get the JS Compiler

You’ll find the jsc.exe (found one on mine in C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727), then add this path to your environment path: Right-click My Computer – Advanced – Environment Variables – System Variables – Path – Edit

Now open command prompt (Start – Run – “cmd” – OK) and type “jsc”+ENTER. You should see a list of help options for the compiler.

First .exe

The last time I created an .exe file is probably years ago, when I was developing a desktop app for a Limosine Management Company in Visual Basic.

Create a file called test.js in C:\\myapps directory with the following content:

var d = new Date();
var n = Math.random();
print('Hello World, \\ntoday is ' + d + '\\nand this is random - ' + n);

Now let’s compile!

C:\\myapps>jsc test.js

No error messages, so we’ll take that as an OK.

Let’s run it.

C:\\myapps>test

That’s so cool, the compiled program works!

Making a DLL

Now, we’re convinced that we have a good thing going here, so let’s create a DLL, meaning create a library that other applications such as Delphi, C#, C++ can use.

JScript.NET has the notion of namespaces and packages and class-based objects. So if we simply wrap our code in a package and a class and we create a new file LibTest.js:

package LibTest{
    class Hello {
        function sayhello() {
            var d = new Date();
            var n = Math.random();
            return 'Hello World, \\ntoday is ' + d + '\\nand this is random - ' + n;
        }
    }
}

Let’s compile this into a library, we need the /t:library option when compiling

C:\\myapps>jsc /t:library LibTest.js

Consuming the lib

Finally, let’s create an app that leverages the new library we just created.

Create consumer.js with the following:

import LibTest;
var h = new LibTest.Hello();
print(h.sayhello());

Compile and run:

C:\\myapps>jsc consumer.js
C:\\myapps>consumer.exe
Hello World,
today is Mon Aug 07 19:53:29 PDT 2017
and this is random - 0.45013379838789525

Turning on/off warnings and errors in PHP

Write a script to determine whether the page is on a local, testing, or live server, and set $state to “local”, “testing”, or “live”. Then:


if( $state == "local" || $state == "testing" )
{
 ini_set( "display_errors", "1" );
 error_reporting( E_ALL & ~E_NOTICE );
}
else
{
 error_reporting( 0 );
}

Or if you are sure your script is working perfectly, you can get rid of Warning and notices by putting this line at the beginning of your php script:

error_reporting(E_ERROR);

Before that, during the development, I would properly debug my script so that all notice or warning disappear one by one. So we should first set it as verbose as possible with:

error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);

To log errors instead of displaying them is to record errors into a file so only the php developer sees the error messages, not the users. A possible implementation is via the .htaccess file, useful if you don’t have access to the php.ini file:


# supress php errors
php_flag display_startup_errors off
php_flag display_errors off
php_flag html_errors off
php_value docref_root 0
php_value docref_ext 0
# enable PHP error logging
php_flag  log_errors on
php_value error_log  /home/path/public_html/domain/PHP_errors.log
# prevent access to PHP error log
<Files PHP_errors.log>
 Order allow,deny
 Deny from all
 Satisfy All
</Files>


Special CSS3 for some browsers

In some special cases when you need special css to handle multiple browsers, here’s how to add new css for each type of browser.

/*this is safari, affects chrome*/
@media screen and (-webkit-min-device-pixel-ratio:0) {
 .uk-logo{
 margin-top:-10%;
 }
}

/* firefox issue */
@-moz-document url-prefix() {
 .uk-logo{
 margin-top:-30%;
 }
}
/*Chrome issues*/
@media screen and (-webkit-min-device-pixel-ratio:0) and (min-resolution:.001dpcm) {
.nav.navbar-nav li{
font-size: 1.53em;
}      </code>

//add more style to handle mobile if needed
@media only screen
and (min-width: 768px)
and (max-width: 992px) {
ul.nav li{
font-size: 1.3em !important;
}
}
}

//add new stylesheet file for Safari
/*Safari issues*/
@media screen and (-webkit-min-device-pixel-ratio:0) {
.nav.navbar-nav li{
font-size:1em;
}
}

//add new stylesheet file for IE
/*IE issues*/
@media screen and (-ms-high-contrast: active), (-ms-high-contrast: none) {
.nav.navbar-nav li a{
font-size: 14.89pt; /*this is perfect &amp; all codes below */
font-size: 15pt; /*remove when done*/
}
}

@media screen and (-ms-high-contrast: active), (-ms-high-contrast: none)
and (min-width: 200px)
and (max-width: 736px) {

ul.nav{
width:98%;
}
}

//add stylesheet for mobile responsive
@media screen and (min-width: 1024px) and (max-width: 1300px) {
//your style here
}
@media screen and (min-width: 992px) and (max-width: 1024px) {
//your style here
}
@media screen and (min-width: 768px) and (max-width: 1024px) {
//your style here
}

@media screen and (max-width: 768px){
//your style here
}

Connecting SFTP Public Keys for Authentication

https://winscp.net/eng/docs/public_key#private

Run SFTP Client on your computer as normal. Using Public Keys for Authentication, you don’t need to use password any more. Enter server Hostname, username, then click on “Advanced” button. Then add the PrivateKey file using SSH=>Authentication. Please Save and login to SFTP.

Once you’re on GC, please look for /var/www/html/. By default, it’ll redirect you to /home/YourUserName/ directory.