The JS file functions properly on Internet Explorer but encounters issues on different browsers

The following function is working in Internet Explorer but not in other browsers. Any suggestions to make it compatible with all browsers?

function displayPacket(str, company) {
    var cam = document.getElementById("company");
    if (window.XMLHttpRequest) {
        var xmlhttp = new XMLHttpRequest();
    } else {
        var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange = function () {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            document.getElementById("packet_1").innerHTML = xmlhttp.responseText;
            document.getElementById("icon_1").innerHTML = "";
        }
    }
    xmlhttp.open("GET", "show_packet.php?car_moto=" + encodeURIComponent(str, true) + "&cam=" + encodeURIComponent(cam.value, true));
    xmlhttp.send();
}

Answer №1

It is advisable to hoist variables and functions to the top of their containing function. Declare your variable once at the beginning, and only assign it within the if/else statement.

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

What could be the reason behind the error encountered while attempting to parse this particular JSON object?

I am encountering an issue with my PHP (Codeigniter Framework) code: $webpages = $this->webpageModel->select('webpageID,webpageTitle')->where('webpagecategoryID', $webpagecategoryID)->findAll(); header('Content-Type: ap ...

retrieve the model value in a listener method triggered by f:ajax or a4j:ajax

Greetings, fellow developers! I'm diving into the world of SO with my very first post. Currently, I am working on a project using JSF2 with Richfaces4 and encountering an issue that has me stumped: Depending on the selected value from a dropdown me ...

Display an image in a DIV by loading it from a URL provided in a TEXT BOX

I am having trouble loading an image into a div tag from a URL that is obtained from a text box. Here is my current code snippet, but I can't figure out what else needs to be done: <html lang="en"> <head> </head> <bod ...

Retrieving the response headers from the core-ajax component

Is it possible to retrieve response headers from the core-ajax polymer element? The code snippet below shows my testing attempt. Although I can see the 'detail' object, there is no response header in the 'xhr' object inside detail; onl ...

What prevents the swapping of arrays within a 2D array using references?

In my coding project, I came across a Matrix stored in a 2D-array: var testM = [ [0.0,2.0,3.0], [1.0,1.0,1.0], [7.0,5.0,6.0] ]; // execute row exchange between a and b function swapRows(a,b){ let temp = a.slice(); // Cloning the value directly rather t ...

Using jQuery to access the ID of a div and create a custom close button

I am trying to implement a close button for my popup DIVs. Each one has a unique ID and I want to hide them by setting the CSS 'display' property to 'none' when closed. However, the following example is not functioning as expected. I a ...

Refresh the form using ajax

I currently have a table that showcases user information, with an "amend" link at the top. Rather than redirecting to another page for updates, I would like to implement Ajax to update the form on the same page. Here is my code snippet: <?php while ...

What is the process for updating or pushing state in NgRx?

Need help with updating the state after adding inputVal. Currently, it only works on the first click and throws this error: Error: TypeError: Cannot add property 1, object is not extensible import { createReducer, on } from '@ngrx/store' import ...

Converting Jquery to Vanilla JavaScript for Bootstrap Scroll Function

I have been attempting to convert this jQuery code to vanilla JavaScript, but I am struggling with it. Can anyone assist me? Here is the jQuery code. The intended functionality is for an effect to toggle as I scroll, but it currently isn't working. I ...

React i18n Express: Unable to access /locales/resources.json (JavaScript / TypeScript / Node.js)

In the process of developing a React application with a NodeJS backend, I originally used Koa as middleware but have since switched to ExpressJS. My current challenge lies in adjusting the i18n translations for Express. Despite following the documentation ...

At what point are functions executed? What methods can I use to manage their execution?

Looking to make a simple call to an API using jQuery's $.get() function as a JS beginner. The issue I'm facing is that the function seems to be called randomly rather than where I expect it in my code. I am open to either doing everything inside ...

Is there a way to execute a cheerp-wasm program without using the 'path' module for loading?

One interesting feature of Cheerp is its cheerp-wasm target, which compiles C++ into both a .js file and its corresponding .wasm file. Essentially, the .js file acts as a loader for the WebAssembly code. This particular loader ...

Eliminate the option to locate columns in the column selector of the mui DataGridPro

Currently, I am creating a data table for a personal project. Utilizing the DataGridPro element from material ui has been quite satisfying, especially with the column selector feature. However, I wish to eliminate the "find column" section. Is it feasibl ...

Experience a seamless front/back DIV transition with a mouseover effect similar to the ones on USAT

Recently, I was tasked with creating a mouseover transition effect for a div similar to the one used on USAToday's website. The structure of the boxes on the site includes: <div class="asset"> <div class="front">'image and some t ...

Can the performance of a system be impacted by node.js cron?

I am looking to incorporate a cron module (such as later or node-cron) into my node server for job scheduling. The jobs in question involve sending notifications (e.g., email) to remind users to update their profile picture if they haven't done so wit ...

Tips for making multiple views function with angularjs' ui-router

I attempted to follow the instructions provided here but I am unable to make it work. https://github.com/angular-ui/ui-router/wiki/Multiple-Named-Views I made sure that my bootstrap grid divs were functioning correctly by placing them all in index.html an ...

Utilizing onBlur and onFocus events in React to implement a dynamic menu that shows or hides

Currently, I am in the process of mastering React and developing a small online electronic store. Within my project, there is a tab labeled "View Store". My goal is for a small, hidden column to appear when a user clicks on this tab, revealing text or imag ...

Exploring the implementation of query parameters in Nest.js

I am currently a freshman in the world of Nest.js. Below is an excerpt from my code: @Get('findByFilter/:params') async findByFilter(@Query() query): Promise<Article[]> { } I have utilized Postman to test this specific router. ht ...

Checking if the current time is within 45 minutes of a specified time and if the present time is later than the specified time using an

I'm currently learning how to work with 'angularJS' and struggling with creating a function that can determine if a given 'time' is within 45 minutes of now, and also if the 'time' has already passed (meaning, if now is a ...

Add the div id to the script

Currently, I have a script set up where by clicking on a specific div (for example id=packtoa), it will add a class 'show' to listview items that have a matching class with the clicked div's id. While this system works well, I find myself n ...