JavaScript - changing object into a string (not functioning properly)

Looking to convert a JavaScript object into a string? Here's an example:

var obj = {"name": "XXX", "age": "27"};

After doing some research, I found out about JSON.stringify(obj);

JSON.stringify(obj); works perfectly when the IE8 modes are set as follows:

Browser Mode : IE8
Documentn Mode: IE8 Standards

However, the same code doesn't work if the settings are different:

Browser Mode : IE8
Documentn Mode: Quirks Mode

It's puzzling why it works in one setting but not the other...

If you have any insights, please share!

Answer №1

If you are working with IE8 and IE8 standards, I suggest using the JSON.stringify method to serialize an object. It is a simple and effective way to convert objects into strings. While most modern browsers support this function, for those that do not, you can use a JavaScript version available here.

Alternatively, if you are unable to fix your IE modes, you can utilize the following custom method to achieve the same result:

Custom Function:

function objToString (obj) {
var tabjson=[];
for (var p in obj) {
    if (obj.hasOwnProperty(p)) {
        tabjson.push('"'+p +'"'+ ':' + '"' +obj[p] + '"');
    }
}  tabjson.push()
return '{'+tabjson.join(',')+'}';
}

Usage of Custom Function:

var obj = {"name": "XXX", "age": "27"};
objToString(obj );

Output:

"{"name":"XXX","age":"27"}"

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

SCRIPT438: The operation failed because the object does not have the ability to use the 'forEach' property or method

Issue with IE8: Property or method 'forEach' not supported $('.tabs').tabs(); $('#search-consumables [data-ajax-call]').change(function() { var $this = $(this), settings = $this.data(), $target = $(setti ...

Error occurred while attempting to run 'postMessage' on the 'Window' object within GoogleTagManager

Recently, I encountered an error stating "postMessage couldn't be cloned". This issue seems to be affecting most of the latest browsers such as Chrome 68, Firefox 61.0, IE11, and Edge. Error message: Failed to execute 'postMessage' on &ap ...

It appears that Yarn is having trouble properly retrieving all the necessary files for a package

Recently, I encountered a strange issue in our project involving 3 microservices, all using the exceljs library. Two of the microservices successfully download all necessary files for this package through yarn. However, the third microservice is missing ...

Error: The specified element to insert before is not a direct descendant of this parent node

As I work on converting a jquery dragable script to plain javascript, I encountered an issue at the insertBefore point. The error message that pops up is: NotFoundError: Node.insertBefore: Child to insert before is not a child of this node. This particula ...

JSON file format in Qt is not valid

Whenever I try to enable Android-Debug mode, this error message pops up: Invalid json file: C:/Qt/Projekt/android-libProjekt.so-deployment-settings.json I have checked sources suggesting that I should update the Qt building path, but the issue remains un ...

What is the most efficient method for managing axios errors in Laravel and Vue.js?

Currently, I am developing spa web applications using Laravel as the backend and Vue.js as the frontend framework. For authentication with API, I am utilizing Laravel Passport, and for managing my application state, I am using Vuex. Initially, I created A ...

The capacity to rotate div containers, adjust their dimensions, and engage with the content within

I'm attempting to design small clickable div boxes that flip 180° when clicked to reveal interactive content. This includes the ability to click links, copy text, or manipulate the content with additional buttons. Although I've successfully ach ...

Using Typescript to implement a conditional return type and ensuring that the value types are consistent together

I am working with a useSelectedToggle hook that helps in connecting the UI state to the open/closed status of a dialog where it will be displayed. The toggle defines the value as (T) when it is open, and null when it is closed. How can I enforce stricter ...

Is it acceptable to use "string" as a variable name in JavaScript?

Any tips on getting the code below to function properly? var x = 'name'; After that, I want to utilize the value inside x like a variable and assign it so that when I call for NAME, I get this outcome: var name = 'NAME'; Can this be ...

Looking for assistance with transferring API information to components

I am currently working on a basic Movie finder application that will display a list of movies containing the name entered by the user. My focus at this stage is to just show the information on the screen. As I'm using React for this project, I have t ...

Inquiries regarding node.js

While delving into research on node.js and Mongodb, I encountered a few areas that require clarification. My aim is to query Mongodb from the web using JavaScript because of my familiarity with the language. Additionally, it aligns well with Mongodb' ...

Use jQuery's AJAX function to upload an array

Having a bit of trouble with a seemingly simple task. I need to send a multi-dimensional array to a php page using jQuery's .ajax function, but I'm struggling to serialize the array correctly. This is the code snippet: var dataToSend = new Arra ...

Having trouble adding a div in React due to the error "Objects are not allowed as a React child"?

While I am rendering the data and displaying it between divs, I keep getting this error: Objects are not valid as a React child (found: Wed Dec 09 1998 00:00:00 GMT+0530 (India Standard Time)). If you meant to render a collection of children, use an ...

Is there a way to immobilize an object in JavaScript without resorting to Object.freeze()?

Is there a way to freeze the following object without relying on Object.freeze()? Let's find out: const obj = { a:'test', b:'Something' } ...

What is the process for clearing the input value of a View from another View?

My situation seems simple and straightforward, yet I am struggling to achieve the desired output. In this scenario, I have two HTML pages named Index.htm and Main.htm, as well as a script page named mscript.js. Index.htm contains a button (Clear), and Mai ...

Invoking a class method through a dynamic function call with the use of setTimeout

In my JavaScript code, I have a structure similar to a class where I'm trying to call a function from the same level using a passed-in function name. Explaining this is a bit challenging, so let me illustrate with an example of what I'm aiming ...

Solve problems with limitations on ng2-dnd drop zones

I successfully integrated drag and drop capabilities into my Angular 4 application using the ng2-dnd library. Within my application, I have containers that can be sorted, as well as individual items within each container that can also be sorted. My goal i ...

What tips can you provide for shrinking the size of an AngularJS website with the help of Google Closure Compiler?

Is there a way to decrease the size of an Angularjs site using Google Closure Compiler? I have a website built on Angularjs 1.8.x and I am interested in compiling it with Closure. Are there any examples or demonstrations available to help me achieve this ...

Guide to: Decoding JSON Data

I've been struggling to deserialize this in C# but so far haven't had any luck. Any help would be greatly appreciated. Here is the JSON data: { "138c399": [ "A1E67B", 39.826, -76.9241, 238, 15400, 402, "2573", "T ...

Issue with using jQuery and parseDouble

I have been dealing with an AJAX request that retrieves a JSON object containing multiple values, each with two decimal points. However, the issue is that when these values are returned as part of the JSON, they are in string format. My goal is to perform ...