Trouble arises when trying to insert a script tag into a live code editor using HTML

A few days ago, I successfully created a live code editor using the ace 1.2.9 library for my website guides. Now, I'm attempting to create a basic example, but when I try to enter text in the designated text area for the guides, the studio code compiler throws errors and fails to load. The issue seems to arise when I add the following line within the text area:

document.getElementById("test").innerHTML = "Hello JavaScript";

Code:

<body onload="ready()">
    <div id="container">
      <div id="editor"></div>
      <iframe id="iframe" frameborder="0"> </iframe>
    </div>
    <script>
      function update() {
        var idoc = document.getElementById("iframe").contentWindow.document;
        idoc.open();
        idoc.write(editor.getValue());
        idoc.close();
      }
      function setupEditor() {
        window.editor = ace.edit("editor");
        editor.setTheme("ace/theme/monokai");
        editor.getSession().setMode("ace/mode/html");
        editor.setValue(

  `  
     <!-- You can test freely     
       with our live code editor. -->

       <html>
<body>

  <h2>Free code wiki</h2>

<p id="test"></p>

<script>
document.getElementById("test").innerHTML = "Hello JavaScript";
</script>

</body>
</html>



`,
          1
        ); //1 = moves cursor to end

        editor.getSession().on("change", function() {
          update();
        });

        editor.focus();

        editor.setOptions({
          fontSize: "16pt",
          showLineNumbers: false,
          showGutter: false,
          vScrollBarAlwaysVisible: true,
          enableBasicAutocompletion: false,
          enableLiveAutocompletion: false
        });

        editor.setShowPrintMargin(false);
        editor.setBehavioursEnabled(false);
      }

      function ready() {
        setupEditor();
        update();
      }
    </script>
  </body>
</html>

Error:

Unterminated template literal

Answer №1

To ensure the correct display, it is necessary to escape the <script> tag as it is within a string

For example:

<script>
...
<\/script>

Answer №2

There are numerous syntax errors present, including 3 missing closing tags. Could you please clarify where the editor.setValue() closing tag is located?

You also have 2 missing closing tags in your code snippet.

May I ask from where you obtained this piece of code?

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The gradual vanishing of principles

Below you will find text fields that I am populating with values that are later utilized in a JavaScript function. These fields are all contained within a form. The issue I am encountering is that when I submit the form, I initially get the correct value ...

Every time Grunt detects newer files, it automatically triggers the imagemin:dynamic task

I am working with a Gruntfile that looks like this: grunt.initConfig({ imagemin: { dynamic: { files: [ src: ['lib/public/img/*.{png,jpg,jpeg,gif}'], dst: 'build/public/img/', expand: true, fl ...

Creating a JavaScript-powered multi-department form: A step-by-step guide

Is there a way to maintain the form displayed on a webpage created in HTML5 and JavaScript for logging calls across three different departments? With buttons assigned to each department, clicking on them reveals the respective HTML form. That said, every t ...

Shorten the content while preserving the HTML using JavaScript

When printing a string containing HTML links, I need to truncate the visible text while preserving the link structure. Simply using a substring method would disrupt the HTML tags in the string. I aim to display only 100 characters but also remove any inc ...

Brother or sister is never empty, as every comment is considered an entity

When going through a list of jQuery objects, I want to verify if the nextSibling attribute is null. However, I found that an HTML comment also counts as a sibling object. How can I address this issue? I am looking for an if statement that will trigger fo ...

Manipulate a value using JavaScript

While the intention is for the button value to switch between 1 and 0, the echo $_POST["ordina"] consistently returns 1. Despite checking the code multiple times, I am unable to identify the issue. <script> function order() { if (document.ordination ...

Add styling to a window popup and close it when focus is lost in Ext JS

I am trying to implement a specific functionality where the popup arrow should be at the edge of the textbox as shown in the image below. How can I achieve this? Additionally, how can I make sure that clicking outside the control destroys the popup instead ...

Assign local variable in Mongoose using query results

I created a function to query MongoDB for a credential (simplified): var query = Credential.findOne({token: token}); return query.exec(function (err, credential) { if (err) return null; return credential; }); The next step is to use this credent ...

Error: The function app.all is not defined

This is a basic Angular page that I have set up. <script type="text/javascript" src="https://code.jquery.com/jquery-1.12.4.js"></script> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script> ...

Guide to sending an ajax request from one page and receiving the response on a different page

Looking for advice on sending Ajax requests using jQuery and HTML5. I have multiple pages within my application. Is it possible to send an Ajax request from one page (e.g sync.html) and receive a response on another page (e.g home.html)? I am aware of alte ...

Please ensure that the form is only submitted when at least one checkbox is selected by utilizing JavaScript

When I visit the following form: Upon filling out the details and clicking submit, if I forget to check a checkbox, a prompt appears stating "please select any one checkbox" with an 'ok' button. After clicking 'ok', the form is then su ...

Tips for updating data within an array using a copied object

Just starting out with JavaScript Could you assist me with a basic inquiry, please? For instance, I have an array like this: const array = [{ id: 1, name: 'Ferrari', type: 'F40', price: '350 000$', countr ...

Develop a professional Angular application for deployment

Help! I'm struggling to build my Angular application for production. After running the 'ng build --prod' command, I can't find all my components in the 'dist' folder. Do I need to change or configure something else? I see som ...

Steps to retain localStorage data while redirecting to another external webpage

Encountering an issue with saving localStorage data across redirects. See the code snippet below: // Invoke newSitelogin(). Save response(credentials) in localStorage. Redirect to new URL. newSitelogin({ uuid, password }) .then(() => { window.ope ...

The user is defined, but the user's user ID is not specified

It seems that the user is defined, but user.user_id is not. My framework of choice is express.js and passport.js. router.post('/requestSale', function(req,res){ console.log('session user: ' + req.session.passport.user); //logs ...

insert the "tr" element directly following the button

I am looking for a way to allow the user to add another input file right next to the button, so they can select and send multiple files at once. https://i.sstatic.net/TI4eJ.png I have tried using various solutions I found during my search but none of the ...

Any ideas on how to fix the error that pops up during the installation of the bootstrap package in Node

The npm command is not recognized as a valid cmdlet, function, script file, or operable program. Please double check the spelling of the command and ensure that the path is correct before trying again. This error occurred at line 1. npm i bootstrap + ...

Oops! Looks like there was an error. The text strings need to be displayed within a <Text> component in the BottomTabBar. This occurred at line 132 in the

My app includes a bottomTab Navigation feature, but I encountered an error: Error: Text strings must be rendered within a <Text> component. This error is located at: in BottomTabBar (at SceneView.tsx:132) in StaticContainer in StaticCont ...

Error encountered: Mongodb db.collection.find() function is not successfully retrieving data, whereas the collection.insert() function

Working with node.js/express and utilizing a Mongodb database to store various data sets. The functionality for adding, editing, and deleting data on a webpage is functioning properly. For instance, the code snippet for adding data is as follows: router. ...

What could be causing the error message to appear stating that each list item must have a unique key when utilizing react-bootstrap in Nextjs?

My file currently contains keys for each child component, but it is still raising an error internally. I am unsure if I can resolve these issues on my own. export default function SecondaryNav(props:NavItems) { const router = us ...