Utilizing deployJava.runApplet for precise element targeting

After years of successfully maintaining an applet that utilizes the traditional:

<script src="foo.js"></script> 

method for embedding a Java applet, we can no longer ignore the need for change.

It's time to switch to:

deployJava.runApplet()

When I invoke this method using a click handler (such as an event listener on a button with jQuery, although the method doesn't matter):

$('#button').click(function() {
  deployJava.runApplet(attributes, parameters, version);
});

...it replaces the entire existing document with the applet. How can I specify a specific DOM element to contain the applet, so my page isn't completely replaced?

I thought there might be an attribute like target: someElement where "someElement" could be a DOM object or the element's ID as a string. However, I haven't been able to find documentation for such an attribute.

Just to provide all information, here are the attributes being passed:

/* A possible location for an applicable attribute */
var attributes = {
  name: "SomeName",
  code: "some.class",
  archive: "some.jar",
  width: 640,
  height: 400
};

var parameters = {
  someParameter: someValue
};

var version = "1.5";

I could use document.write to reconstruct the document, but you can understand why I'd prefer to avoid that approach.

Any guidance would be much appreciated.

Answer №1

Instead of following the solution provided by Christophe Roussy, you can opt for a different approach by overriding the document.write function while executing deployJava.runApplet.

Here's how you can do it:

function wrapDocumentWrite(func) {
    var writeTo = document.createElement('ins'),
        oldwrite = document.write,
        content = '';
    writeTo.id = "you";
    document.write = function(text) {
        content += text;
    }
    func();
    writeTo.innerHTML += content;
    document.write = oldwrite;
    document.body.appendChild(writeTo);
}

Then, use this wrapper like so:

wrapDocumentWrite(function () {
    deployJava.runApplet(attributes, parameters, "1.6");
});

This method may seem a bit unconventional but it gets the job done effectively!

Answer №2

The main issue at hand revolves around deployJava.js utilizing the document.write method. Utilizing this method after the page has already been rendered results in clearing the document, leading to obvious negative consequences.

While initially designed for JavaFX, some individuals have reported successful outcomes with dtjava.js, presenting it as a potential alternative worth exploring.

Despite the viability of dtjava.js, certain team members have invested time and effort into working with deployJava.js and are hesitant to abandon their progress. This required me to adhere to using deployJava.js exclusively, ensuring that it is invoked during the page rendering process rather than through Ajax requests or delayed triggers.

In practice, we gather necessary information and pass it onto a secondary page responsible for rendering the applet accordingly. This approach proves effective, especially considering that our clients typically follow a similar pattern of data collection, server-side processing, and receiving a redirect response. While we currently utilize query strings for passing information, alternatives like cookies or the localstorage API could also be considered for maintaining a cleaner-looking window.location.

I appreciate the input provided, even if shared within the comment section. Additional suggestions are welcome as we remain open to exploring better practices!

Answer №3

If you're working with jQuery and need to specifically target a particular DOM element instead of simply appending content:

function customAppend(jq, func) {
    var oldwrite = document.write,
        content = '';
    document.write = function(text) {
        content += text;
    }
    func();
    document.write = oldwrite;
    jq.html(content);
}

customAppend($('#specificDiv'), function () {
    deployJava.runApplet(attributes, parameters, version);
});

Answer №4

In order to resolve this frustrating issue, I took the step of downloading and modifying deployJava.js at line 316. I substituted the existing line with my customized code:

  // document.write(n + "\n" + p + "\n" + r);
  myDiv.append(n + "\n" + p + "\n" + r);

The variable myDiv in the script refers to a global JavaScript variable that has been assigned to the specific div element before executing the runApplet function:

myDiv = jQuery('#someDiv');

If you come across a less invasive solution, please feel free to share it with me...

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

Displaying PHP output within a JavaScript expression

Let's dive into a scenario involving a basic HTML document and some JavaScript that's loaded within it: <!-- doc.html --> <!doctype html> <html lang="en"> <head> <script type="text/javascript" src=" ...

Equivalent of JMS Topic in C/C++ Messaging Libraries

I need some guidance on how to connect to MQ using TOPICS in C++. I have tried looking through IBM documentation but couldn't find a clear explanation. In Java, it seems you can configure subscriptions via the Websphere Control Panel, but how do you d ...

Looking for a discreet JavaScript-based text editor with advanced features?

Our CMS has been using the now-unsupported RichTextBox control for a while, and we want to find a lighter-weight alternative with better cross-browser support. Initially, we were considering different ASP.NET components, but I am starting to think that an ...

A pair of div elements within one section

Can you explain why the following HTML code uses two divs, one with a class and the other with an ID, instead of just using one of them to assign properties? .header { background-image: url("https://s3.amazonaws.com/codecademy-content/courses/web-101/ ...

Update the canvas box's color when you interact with it by clicking inside

I'm in the process of developing a reservation system and I'm looking to implement a feature where the color of a Canvas changes when clicked. The goal is for the color to change back to its original state when clicked again. Snippet from my res ...

Error encountered: TypeError: __webpack_require__.t is not a function - Issue appears only on live server, while localhost runs without any problem

I set up an e-commerce site that runs smoothly on localhost. However, when I deploy it to a live server, an error appears. Interestingly, the error disappears after reloading the page. Here is the code snippet: import React, { useEffect, useState } from & ...

webdriverIO encountered an unhandled promise rejection, resulting in a NoSuchSessionError with the message "invalid session id

I am currently learning how to conduct UI testing using Jasmine and WebdriverIO in conjunction with NodeJS. Below is a snippet of my test code: const projectsPage = require('../../lib/pages/projects.page'); const by = require('selenium-we ...

Eliminate the Div Attribute once the radio button is activated

I'm just starting out with JavaScript and feeling a bit lost. I came across some code that adjusts the attributes of a Div based on a selection from a dropdown list. Now, I want to tweak it so that it works when a radio button is selected instead. Be ...

Wheelnav.js implementing a dynamic bouncing animation

I'm currently in the process of working on a pie menu with wheelnav.js and everything is going smoothly so far. However, I can't seem to find any information in the wheelnav.js documentation on how to eliminate the bouncing effect when a menu cho ...

Using jQuery.ajax to manage caching and manipulating timestamps

Currently, I am utilizing the jquery.ajax techniques to transmit GET-requests to the server, with caching disabled by setting cache to false. Everything seems to be working fine so far. It appears that jQuery automatically includes a timestamp (likely epo ...

Creating a three-column layout with position fixed in the center

I'm struggling with achieving a maximum width of 400px for the entire set of three columns and centering them. ---------------------------------------------------- | | | |----------------------------- ...

How to manage print preview feature in Firefox with the help of Selenium in the Robot Framework

Attempting to select the 'cancel' button in the print preview page on Firefox has proven to be a challenge. Despite my efforts, I am unable to access the element by right-clicking on the cancel option. Interestingly, Chrome allowed me to inspect ...

Unable to render data in Chart JS using PHP JSON

Hello, I’m currently working on creating a dynamic line chart using Chartjs. The data is being pulled from an SQL database using PHP in JSON format. Although the data is successfully retrieved, the chart appears blank. Any assistance would be greatly app ...

Struggling with Bootstrap 4 - need help with navbar and flexbox alignment issues

My goal is to recreate a navigation bar similar to this design: navbar I initially attempted to use the grid system for my navbar, but it didn't work out as expected. After some research, I found that using the grid system for navbars is not recomme ...

Integrating redux-form with the react-widgets DateTimePicker component

I am currently working on a project using ReactJS with Redux and I am trying to incorporate a form similar to the one shown in this example: Most of the components are working well, but I am encountering an issue with the DateTimePicker due to the momentL ...

Arrange data into columns on a website

In my project, I'm exploring the challenge of creating a square 2 x 2 grid alongside a rectangular column on the right-hand side. While attempting to implement a grid system for the 2 x 2 layout, I encountered issues with the alignment of the rectang ...

Can you recommend a straightforward method in Vue.js for validating the format of user input?

Here is how I have implemented an email sending feature in Vue.js: <template> <button @click="sendMail(); $emit('close')">Send</button> </template> <script> methods: { sendMail () { axios ...

Exploring the differences between UTC and non-UTC date formats in Javascript

When working with JavaScript, I encountered a challenge in comparing two dates that are formatted differently. Specifically: 2015-09-30T00:00:00 and 9/30/2015 12:00:00 AM The former is in UTC format while the latter is not. Despite referring to the same ...

The functionality of a JQuery post within a PHP loop is not functioning optimally

I am encountering an issue with my list.php file, which generates a list with items and a button (Erledigt!) that triggers a jQuery Post to wishdelete2.php. The problem is that the jQuery post sometimes requires multiple clicks (3x or 5x) before it process ...

The JavaScript array slideshow is experiencing some glitches in its transition

After diving into JavaScript recently, I managed to center a div and make it fullscreen as the window resizes. However, things got tricky when I added a script I found online to create an image transition using an array. Unfortunately, these scripts are co ...