CloudEmbedding Interactive Code onto a Web Page

Embedding Interactive Code onto a Web Page content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

Have you ever created an online training manual for your new developers or end users? Have those manuals ever included sample code listings or snippets for the developer or end user to see? Have you ever written an article that you wanted to share with the world on a site like or a tip for that included a self-contained code listing you wanted to share?

If so, odds are the code listing was presented in a static manner. For example, a Java listing could be presented similar to Listing 1:

Listing 1: A Java Main class

class Main {
  public static void main(String[] args) {
    Adder Myadder = new Adder();
    System.out.println(Myadder.add("Hello ", "World!"));    
    System.out.println(Myadder.add(101, 202));
    System.out.println(Myadder.add(1.5, 40.5));
    System.out.println("We did it!");

Of course, the Java program in Listing 1 relies on a second file to run that contains code for the Adder class methods, which are presented in Listing 2:

Listing 2: The Adder class

public class Adder {
  public int add(int x, int y) {

  public String add(String x, String y) {

  public double add(double x, double y) {

With the two listings in place, a reader is able to copy and paste the code into their favorite editor and then use a compiler to build and run the program. In fact, you could copy and paste the two listings into your favorite Java IDE, compile them, and run the program. When you do, you’d see results similar to the following:

Hello World!
We did it!

If only there was an easier way! In fact, it would be so much better if you didn’t have to leave this page to try the code. After all, once your attention shifts to your own IDE, you might not come back!

Using and Embedding Code

There are several tools that let you actually embed code into a page. We’ll cover in this article and show you how easy it is to use. is an online development tool that lets you use any of over 50 languages to develop online without installing anything locally. To get an idea of what you can do with, check out the article An Online Editor for Coding or Learning here on As you’ll see, it is a free tool that can be used from most browsers and mobile devices.

Once you have established an account on (see the previously mentioned article), you’ll be able to create projects (called repls) where you can enter code, compile it, and run it right on the site. The tool allows you to share the code with others. This sharing can be collaborative or through creating a copy (fork) of the code. The two Java listings mentioned earlier have both been created using

You can also embed a project onto a web page just like this:

If you look at the above embedded object, you are going to see that it provides a lot of functionality. It contains the code listings, a run button, a console pane, and more. Clicking on the run button will display the results of compiling and executing the code in the console pane.

You can also change the code! To change the code, however, you’ll need to log into a account or create one to use. A copy of the code will then be put into your account for you to make changes. That allows the original copy on the article to remain pristine for other readers.

The embedding is done using iFrames that hold For the above embedded code window, the following code was included on this article:

<iframe height=”400px” width=”100%” src=”” scrolling=”no” frameborder=”no” allowtransparency=”true” allowfullscreen=”true” sandbox=”allow-forms allow-pointer-lock allow-popups allow-same-origin allow-scripts allow-modals”></iframe>

This iFrame code can be automatically generated from the site. When you have your project opened within, you can select the Share button in the upper right side of the browser window. This will pull up the sharing dialog as shown in the following figure:

Simply click the “<> Copy embed code” button and the iFrame code will be copied to the clipboard. You can then paste it from there into your web page’s HTML code.

As you can see by looking at the embed code presented earlier, the iFrame contains a few parameters that can be changed, including borders and the size of the window. There is also a link to jump to the site. In order for the repl (the embedded object) to work, you must keep the “?lite=true” parameter on the embedded link. If you only want to show the running code, you can hide the editor by adding “?outputonly=1” to the embedded URL. Note, however, that the user can still open the project on the site and get access to the code.

That’s all there is to it. With just a few steps, you gain the ability to add interactive code to your user manuals and other online products!

Note: On the Medium platform, you can simply paste a project URL into a page and it will convert it to the interactive environment automatically. On WordPress, you may need to make sure that is on your approved whitelist of sites. Additionally, if you paste a project URL into WordPress and choose embed if prompted, then it will automatically format a project URL to the embed code for you.

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Latest Posts

Related Stories