Showing posts with label javascript. Show all posts
Showing posts with label javascript. Show all posts

Expose module in global scope using Browserify or Webpack

Tags: October 25, 2019 6:05 AM
0 comments

Goal

You want to expose a module as in global scope so it can be called in HTML file. For example we will create a small function for reversing a string. We will expose it as StrReverse.

// File main.js
module.exports = function(str) {
  return str.split('').reverse().join('');
}

Browserify

$ browserify --standalone StrReverse main.js --outfile bundle.js

The key is --standalone parameter.

Webpack

$ webpack-cli --mode=none --output-library StrReverse main.js --output bundle.js

The key is --output-library parameter.

Test in HTML

Create a HTML file and include bundle.js via <script> tag.

<!DOCTYPE html>
<html>
<body>
<script src="bundle.js"></script>
var reversed = StrReverse("Hello World");
document.write(reversed);
</body>
</html>

Share on Facebook Twitter

Decode HTML Entities using Javascript

Tags: February 23, 2014 1:13 PM
0 comments

I came into situation where I need to pass some string to Pen Editor instance. The problem is the string is already encoded to HTML entities by PHP's htmlentities(). So, when I have value like This is &lt;strong&gt;strong&lt;/strong&gt; element. The Pen editor instance convert it into:

This is <strong>strong</strong> element

Instead of this:

This is strong element

The Solution

The solution is pretty dead simple just inject the string into textarea element and call the value property to get the content instead of innerHTML.

function decodeHtml(html){
  var txt = document.createElement("textarea");
  txt.innerHTML = html;
  return txt.value;
}
If your string already came from textarea like in my case then you don't event need to create a single function.

References

Share on Facebook Twitter

Compile SpiderMonkey Ubuntu Linux

Tags: January 4, 2013 11:23 PM
0 comments

Install terlebih dahulu paket-paket dependency yang diperlukan sebelum melakukan kompilasi.

# apt-get install nspr4-dev autoconf2.13
Download source SpiderMonkey 1.8.5 pada http://ftp.mozilla.org/pub/mozilla.org/js/js185-1.0.0.tar.gz.
# cd /tmp/
# wget http://ftp.mozilla.org/pub/mozilla.org/js/js185-1.0.0.tar.gz
# mkdir spidermonkey
# tar -zxvf js185-1.0.0.tar.gz -C /tmp/spidermonkey
# cd spidermonkey/js-1.8.5/js/src
# autoconf2.13
# ./configure
# make
Setelah selesai akan terdapat file binary pada shell/js, dimana file tersebut adalah Javascript Shell Interpreter yang menggunakan engine SpiderMonkey.
# shell/js
js> for (var i=0; i<3; i++) {         
print("notes.rioastamal.net");  
}
notes.rioastamal.net
notes.rioastamal.net
notes.rioastamal.net

js> quit()

Share on Facebook Twitter