小能豆

Run a JS script on page load

javascript

I have a .js file that is the javascript translation of a java file.jar. I would like to run the file as soon as the HTML page is loaded. Is it possible? now the file is run using the following code

 <div class="w3-twothird" align="center">
       <script>
           cheerpjInit();
           cheerpjCreateDisplay(500,400);
           cheerpjRunJar("/app/tesi/homepage/disgrafia.jar");
       </script>
       </div>

I’d like to use only the js file and not the jar.


阅读 68

收藏
2023-11-30

共1个答案

小能豆

If you have a JavaScript translation of a Java file and you want to execute it when the HTML page is loaded without relying on the original .jar file, you can include the JavaScript code directly in your HTML file or link to the external JavaScript file. However, it’s important to note that the translation should be compatible with running in a browser environment.

Here’s an example of how you can include the JavaScript code in your HTML file and execute it when the page is loaded:

  1. Include the JavaScript code directly:
htmlCopy code<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Your HTML Page</title>
</head>
<body>
    <div class="w3-twothird" align="center">
        <script>
            // Include your JavaScript translation code here
            function cheerpjInit() {
                // Implementation of cheerpjInit
            }

            function cheerpjCreateDisplay(width, height) {
                // Implementation of cheerpjCreateDisplay
            }

            function cheerpjRunTranslation() {
                // Your translated JavaScript code here
                console.log("Executing the translated JavaScript code...");
            }

            // Call the initialization functions
            cheerpjInit();
            cheerpjCreateDisplay(500, 400);

            // Run the translated code
            cheerpjRunTranslation();
        </script>
    </div>
</body>
</html>

Replace the placeholders (cheerpjInit, cheerpjCreateDisplay, and cheerpjRunTranslation) with the actual translated JavaScript code you have.

  1. Link to an external JavaScript file:

If your translated JavaScript code is in a separate file (let’s say translated_code.js), you can link to it in your HTML file:

htmlCopy code<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Your HTML Page</title>
</head>
<body>
    <div class="w3-twothird" align="center">
        <!-- Link to the external JavaScript file -->
        <script src="/path/to/translated_code.js"></script>

        <script>
            // Call the initialization functions
            cheerpjInit();
            cheerpjCreateDisplay(500, 400);

            // Run the translated code
            cheerpjRunTranslation();
        </script>
    </div>
</body>
</html>

Make sure to replace /path/to/translated_code.js with the actual path to your translated JavaScript file.

Remember to handle any dependencies or environment-specific considerations that might be required for your translated JavaScript code to run successfully in a browser.

2023-11-30