• Home
  • New Entries
  • Popular Entries
  • Submit a Story
  • About

How to convert HTML to Javascript (.js) in Java ...

For some security reasons, you may need to convert your HTML file into Javascript (js) file, and display the JS file instead of the HTML file directly. The concept is quite simple – using the document.write


HTML

<h1>Convert HTML to Javascript file</h1>

Javascript (js)

document.write(<h1>Convert HTML to Javascript file</h1>);

1. Test.html

Create a simple HTML file, convert this file to Javascript later.

<html>
<body>
<h1>Convert HTML to Javascript file</h1>
</body>
</html>

2. ConvertHTMLToJs.java

Create a Java class to convert all the HTML code into a Javascript (.js) file.

package com.mkyong.io;
 
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.OutputStream;
import java.io.PrintStream;
 
public class ConvertHTMLToJs {
 
 private static final String FOLDER = "c:";
 private static final String JS_FILE_NAME = "output.js";
 private static final String HTML_FINE_NAME = "test.html";
 private static final String JS_PREFIX = "document.write(";
 private static final String JS_SUFIX = ");";
 
 public static void main(String[] args) {
 
      try {
 
       //read html file
          BufferedReader br = new BufferedReader(new FileReader(FOLDER + File.separator + HTML_FINE_NAME));
 
          //output it to js file
          OutputStream os= new FileOutputStream(new File(FOLDER + File.separator + JS_FILE_NAME));
          PrintStream ps = new PrintStream(os);
 
          StringBuffer sb = new StringBuffer();
          String line;
 
          while ((line = br.readLine())!= null) {
 
              line = sb.append(JS_PREFIX).append(line).append(JS_SUFIX).toString();
              //clear the StringBuffer content
              sb.delete(0, sb.length());
 
              ps.println(line);
          }
 
          ps.close();
          os.close();
          br.close();
 
          System.out.println("done");
 
          }catch(Exception e) {
           e.printStackTrace();
          }
     }
}

3. Output.js

Run the above Java program, it will convert “Test.html” to “Output.js”

document.write(<html>);
document.write(<body>);
document.write(<h1>Convert HTML to Javascript file</h1>);
document.write(</body>);
document.write(</html>);

4. Test It

Create a HTML file and include the “Output.js” file for display.
Test-js.html

<html>
<body>
<script type="text/javascript" src="output.js"></script>
</body>
</html>

You will notice both “Test.html” and “Test-js.html” are display the same content, but with different methods to display it.

 Original Source:
http://www.mkyong.com/java/how-to-convert-html-to-javascript-js-in-java/

AddThis Social Bookmark Button

Posted at 10:48:17 am | Permalink | Posted in Java  Javascript  

Related Stuff

  • MooV: Using cutting edge Video phones and Software Video Phones - coupling all that with VoIP and empowering the disabled.

  • Moo Telecom: VoIP communications made easy - Ring anyway with the fun and ease of using a normal phone

  • TagR:Mobile Social Network with Real Time Locations Based services, and Ambience Intelligence, VoiP, IM, Skype, Googletalk, Mapping, Flickr, Events, Calendaring, Scheduling, SecondLife Support

  • ClearSMS : ClearSMS is a Web-based application that lets you send bulk SMS messages to your customers, contacts, or just about anyone.

  • Jajah:jah is a VoIP (Voice over IP) provider, founded by Austrians Roman Scharf and Daniel Mattes in 2005[1]. The Jajah headquarters are located in Mountain View, CA, USA, and Luxembourg. Jajah maintains a development centre in Israel.

  • Skype: It’s free to download and free to call other people on Skype. Skype the number one voice over ip software

  • PrivatePhone: a free local phone number with voicemail and messages you can check online or from any phone.

Top Stuff

MessengerFX

e-messenger

ILoveIM

Top 20 Ruby CMS

MSN Web Messenger

eBuddy



About Ajaxlines

Ajaxlines is a project focused on providing its audience with a database of most of Ajax related articles, resources, tutorials and services from around the world.

Its purpose is to showcase the power of Ajax and to act as a portal to the Ajax development community.


Search


Topics

  • .Net (171)
  • Ajax (89)
  • Ajax Games (10)
  • Articles (95)
  • Bookmarking (35)
  • Calendar (21)
  • Chat (45)
  • ColdFusion (3)
  • CSS (79)
  • Email (23)
  • Facebook (84)
  • Flash (19)
  • Google (54)
  • Html (28)
  • Image (11)
  • International Calls & VOIP (7)
  • Java (56)
  • Javascript (271)
  • jQuery (171)
  • JSON (70)
  • Perl (2)
  • PHP (162)
  • Presentation (19)
  • Python (3)
  • Resources (2)
  • RSS (8)
  • Ruby (31)
  • Storage (4)
  • Toolkits (103)
  • Tutorials (224)
  • UI (11)
  • Utilities (174)
  • Web2.0 (18)
  • XmlHttpRequest (28)
  • YUI (13)

© 2006 www.ajaxlines.com. All Rights Reserved. Powered by IRange