Modifying a JavaScript code with document.write(variable)

I am working with a Javascript function

function setComparison(data) {
    var w= window.open('', 'comparison', 'width=600, height=400');
    w.document.open();
    w.document.write(getComparisonContent(data));
    w.document.close();
    return false;
}

Although I do not have access to the code that generates the (data), I am in need of replacing a specific string within it. Is there a method available where I can pass the (data) through a string replace function?

Answer №1

data.replace() appears to be the most suitable solution...

function setComparison(data) {
    var w= window.open('', 'comparison', 'width=600, height=400');
    w.document.open();
    data = data.replace("foo", "bar");
    w.document.write(getComparisonContent(data));
    w.document.close();
    return false;
}

In this scenario, foo will be substituted with bar

Answer №2

The solution provided earlier did not yield desired results, however I found success with the following approach:

function updateComparison(data) {
    var newWindow= window.open('', 'comparison', 'width=600, height=400');
    newWindow.document.open();
    replacedHtml = replacedHtml.replace("text to be replaced", "new text");
    w.document.write(getUpdatedComparisonContent(data));
    w.document.close();
    return false;
}

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

Email responses containing unidentifiable values

Currently, I am working on implementing AJAX for my contact form to send form data to the server. The objective is to have the server email me the user's information extracted from the input fields. However, I'm encountering an issue where the f ...

Expand the data retrieved from the database in node.js to include additional fields, not just the id

When creating a login using the code provided, only the user's ID is returned. The challenge now is how to retrieve another field from the database. I specifically require the "header" field from the database. Within the onSubmit function of the for ...

Javascript Object and Conditional Statement

I am working on a Dygraph object creation script that includes a data variable. However, I want to avoid passing in this variable for older browsers. Here is the code snippet: function prime() { g = new Dygraph( document.getElementById("g"), d ...

Typehead.js on Twitter is displaying the full query instead of just the value

The Problem This is the issue I am facing with my code. My goal is to retrieve only the value, but instead of that, the entire query value is being returned. var engine; engine = new Bloodhound({ local: [{value: 'red'}, {value: 'blue&apo ...

Modal window closed - back to the top of the page

I am struggling with a simple modal popup window that does not maintain the scroll position when closed, instead returning you to the top of the page. I am looking for a way to keep it at the same scroll position without much knowledge of Javascript. You ...

Use jQuery to retrieve the number of items that have been selected in an ASP ListBox

Struggling to find a way to calculate the count of selected items from an ASP listbox using jQuery. Currently, encountering an issue where I am receiving "Cannot get property length of undefined or null reference" on the selectedOptions property. The var l ...

Adding Multiple Items to an Express Endpoint

I have a requirement to store multiple objects in my mongo database within an express route. Currently, the process is smooth when I post individual objects (such as ONE casino), as shown below. Instead of repeating this numerous times, I am seeking assist ...

What steps can I take to avoid keypress events causing issues with the browser's input functionality?

Utilizing Bootstrap's modal component, I have implemented an "Add User" dialog within my web application. In order to streamline the user experience and enable quick data entry, I am aiming for the escape and enter keys to close and submit the form re ...

Retrieving all rows from a table using Laravel API and Vue.js

<template> <div class="container"> <div class="row mt-5 mb-3"> <div class="col-md-10"> <h3>Gallery</h3> </div> <div class="col-md-2"> <button class="btn btn-success" ...

Creating a ROT13 cipher in JavaScript

In my JS function, I need to handle a variable called i. My goal is to encode this variable using ROT13 before proceeding with the function. The challenge lies in decoding the variable and integrating it into the script. I came across a JavaScript implem ...

Automatically append a version number to destination output files using the Grunt task runner

We have a package.json file that contains our version number, like this: { name: "myproject" version: "2.0" } The objective is to dynamically insert the version number from the package.json file into the output files. Instead of manually updating ...

Challenges in creating an alternative path in ExpressJS

I am currently working on a website for my studies. I decided to use nodejs/Express, as the technology is free. The first route /home was successful, but I am having trouble creating more routes. Although I thought I had a good understanding of the system ...

What is the reason for handlers not being able to work without passing parameters in React?

I recently made a discovery but I'm still puzzled about how it works. In the past, when creating React components, I used to follow this pattern: class App extends React.Component { state = { input: '' } onChangeHandler = event = ...

Reboot JavaScript once a day for optimal performance

Is it possible for the JavaScript's ?random to be based on the current date so that it only loads once a day? <script type="text/javascript" src="http://external.example.com/bookmarklet.js?random"></script> I am asking this question beca ...

Issue with Google Maps API - Error: google object is undefined

Currently, I am in the process of learning how to develop Google Maps by analyzing some basic examples provided by Google. In my Angular 9 application, I utilize Angular Google Maps to showcase a Google Map. However, as I implement this feature, I encounte ...

Exploring the implementation of useMediaQuery within a class component

Utilizing functions as components allows you to harness the power of the useMediaQuery hook from material-ui. However, there seems to be a lack of clear guidance on how to incorporate this hook within a class-based component. After conducting some researc ...

The conversion of the input (ImageButton) to jQuery is not possible

Within my gridview, I have ImageButtons that open an edit form. To determine the ID, I need to access the surrounding element (Row). The issue arises when I try to pass 'this' as a parameter in the onClientClick event which triggers a JavaScript ...

Checking with Protractor to see if the modal is displayed

I am currently working on a Protractor test to check if a bootstrap modal window that confirms the deletion of a record is visible at this time. The record that needs to be deleted is displayed in an angular ng-repeat, so I have to trigger the delete butt ...

What exactly is the purpose of measuring "First Load JS" in the @next/bundle-analyzer tool?

The analysis generated by the NextJS bundle analyzer appears as follows: Page Size First Load JS ┌ λ / 12 ...

How to retrieve a property with a colon in JavaScript object

After parsing XML into JSON with xml2js, I found elements with colons like this: obj = { "data:example":"smthing"}; Is there a way to directly access these elements in JSON? Any tips or tricks? Thank you! ...