``Is there a way to refresh the CSS, button, or a specific part of a webpage after a GET request has been

Upon receiving a GET request, the values appear and the button disappears. View image here.

The button functions properly on localhost. See it in action here.

I'm attempting to incorporate a Facebook share button on a page with dynamic content like userid from an esp826 server. I am using JavaScript, specifically innerHTML with an AJAX call for a JSON object when the page body loads. Testing locally works fine as the JSON file loads quickly before the button, ensuring everything runs smoothly. However, when using the esp8266 server, there is a delay in the response causing the button to disappear once the values are populated. It seems like the CSS needs to be reloaded via JavaScript to revive the button box.

Is there a way to refresh the button?

The contents of the .json file are as follows:

{"temp1":"1", "energia":"2", "energiatotal":"3", "tem":"2", "cliente":"22", "usuario":"22"}

To test locally, place the getajx.json file containing the above content in the same directory as the HTML page. It will work, but I need help resolving issues with long GET requests. Any suggestions?

I tried adding a flag after receiving a positive response and activating the reloadCss function, but it didn't work.


<script>
var temp1, energia, energiatotal, tem, cliente, usuario ;
var ok=0;
function GetAjx() {
    var xmlhttp = new XMLHttpRequest();
        xmlhttp.onreadystatechange = function() {
            if (this.readyState == 4 && this.status == 200) {
                ok = 1;
                var myObj = JSON.parse(this.responseText);
                document.getElementById("temp1").innerHTML = myObj.temp1;
                document.getElementById("energiatotal").innerHTML = myObj.energiatotal;
                document.getElementById("tem").innerHTML = myObj.tem;
            }
        };
        if (ok = 1) {    
            function reloadCss(){
                var links = document.getElementsByTagName("link");
                for (var cl in links){
                    var link = links[cl];
                    if (link.rel === "stylesheet")
                        link.href += "";
                    }
                }
            };  
        xmlhttp.open("GET", "getajx.json" , true);
        xmlhttp.send();
}
</script>

Answer №1

I managed to solve the issue at hand...

It turns out I needed to rearrange the order of the scripts so that the jQuery Ajax source loads first. I also tried placing the Facebook script at the bottom of the page...

I removed any reload scripts and other Facebook scripts that I had been experimenting with

Most importantly, I changed the get request to false in order to make it synchronous instead of asynchronous. This made the page wait for the get request to finish... Interestingly, I had tried this before without success, but now it seems to work possibly due to the order of the scripts. Any thoughts on this?

xmlhttp.open("GET", "getajx.json" , false);

Using the F12 tool on Google Chrome, specifically the performance tab, was quite helpful

I ultimately decided not to tamper with the priority of the scripts, even though I had attempted to do so

If anyone has any comments, I would greatly appreciate them

Once again, thank you for the assistance

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

Identifying the presence of vertical scrolling

Is there a way to achieve the same functionality in JavaScript without using jQuery? I am looking to detect the visibility of the scrollbar. $(document).ready(function() { // Check if body height is higher than window height :) if ($("body").heigh ...

Is it usual for the container to overflow with div content?

Is this common CSS behavior? How can we make the container wrap around the button? http://jsfiddle.net/afrontrow/yepw7oLw/ CSS: .button { background: grey; padding: 10px 20px; } .container { border: 1px solid black; } HTML: <div class= ...

Encountering issues with creating a session in Selenium/webdriver while utilizing Safari 12

Ever since making the update to Safari 12, my automated scripts have been encountering a new error: SessionNotCreatedError: Request body does not contain required parameter 'capabilities'. (Interestingly, this error is exclusive to Safari and d ...

The datatables button triggers an event twice

Whenever I click a button or tag in datatables, the modal opens twice and ajax runs twice. PS. I am using angular.js with datatables as a directive that is created by jQuery datatables, not the Angular datatables module. How can I solve this issue? Than ...

Display a button with an icon only when in responsive mode using HTML, CSS, and Materialize

Is there a way to hide the text on a button but keep the icon visible when viewing on a mobile device? For instance, if a button displays a face icon from MaterializeCSS along with the text "support", I would like to eliminate the text while still showing ...

Establish a connection with the hivemq broker using MQTT protocol

Within my app.js file: const mqtt = require('mqtt') const client = mqtt.connect('mqtt://localhost:1883') topic = 'testTopic' client.on('connect', ()=> { client.subscribe(topic) }) client.on(&a ...

What strategies can be implemented to enhance accessibility for custom table behavior?

Our team is currently working on a customized web application for a client's specific needs regarding data tables. The requested functionality includes the ability to sort table columns by clicking on the header, which has already been successfully im ...

Attaching identical class and event handlers to numerous dynamically created elements

I am facing a challenge with the following HTML structure: <a href="#" @click.prevent="toggleClass">Show/Hide</a><br> <li :class="{myClass: showItems}">Item 1</li> <a href="#" @click.prevent="toggleClass">Show/Hide< ...

Using async/await in React to retrieve data after a form submission

I am currently facing an issue with displaying data fetched from an API on the screen. My goal is to retrieve data when a user types something in a form and clicks the submit button. The error message I am encountering is: TypeError: Cannot read propert ...

Suggestions for the AJAH Library

Currently, my application is built on classic ASP. I am interested in incorporating AJAX-style partial page updates to improve the user experience and avoid unnecessary server roundtrips. My goal is to have a list of rows displayed with the option to add ...

Checking for the presence of a specific function in a file using unit testing

I am curious if there is a possible way to utilize mocha and chai in order to test for the presence of a specific piece of code, such as a function like this: myfunction(arg1) { ...... } If the code has been implemented, then the test should return t ...

Holding off $ajax requests until specific code finishes executing

I'm facing an issue with incorporating geolocation data into my $ajax call URL. Currently, both console.log(lat/lon) calls return the initial value of 0, indicating that the geolocation call is too late to provide the updated values. This results in t ...

Divide a JavaScript project into multiple packages using either webpack or npm

I am embarking on the development of an application that needs to be compatible with Windows (PC), Android, and iOS. To achieve this, I plan to use Electron for Windows and React Native for mobile platforms. Both applications will be built using React and ...

What is the best way to access the iframe element?

This is main.html <body> <iframe id="frame" src="frame.html"></iframe> <script type="text/javascript"> document.getElementById('frame').contentWindow.document.getElementById('test').innerHtml = &apos ...

Switching the active className in React and Next.js based on selection status

My goal is to dynamically change the className of each Card component when clicked on. When one Card is selected, the others should revert back to their default className. This is my UserBookingData component: const UserBookingData = ({ bookings }: any) = ...

Select all feature in checkbox is malfunctioning

I need to implement a feature with 3 checkboxes: When the "A" checkbox is clicked, only "A" should be highlighted. Upon clicking the "C" checkbox, only "C" gets highlighted. If the "B" checkbox is clicked, both "A" and "B" nee ...

What could be causing the issue with converting a Firestore timestamp to a Date object in my Angular app?

Currently, I am developing an Angular project that involves using a FireStore database. However, I have encountered a problem with the setup. Within my Firestore database, I have documents structured like the example shown in this image: https://i.sstatic ...

Timetable sets anchors raised

Is there a way to set up regular sail lifts? For instance, can sails be scheduled to restart every 12 hours? I am looking to do this to prevent losing connection to the remote database. Thank you ...

Avoid causing the newline character to display

var i = 'Hello \n World' console.log(i) /* Output: Hello World */ /* Desired output: Hello \n world */ var j = 'javscr\u0012ipt' console.log(j) /* Output: javscr ipt */ /* Desired output: javscr\u0012ipt */ ...

I wish to substitute a form field with a different one once the value of another field matches a specific condition

The issue I'm facing is that the field disappears once "US" is chosen and does not revert back to the options when another choice is made. teacher_signup.html {% block content %} <h2>Sign Up As Teacher</h2> <form method="post> ...