What is the best way to extract value from an innerHTML object?

Is there a way for me to retrieve a parameter from a JSON page? I have the ability to run code on that page.

alert(document.body.innerHTML)

When this code is executed, it displays:

<pre style="word-wrap:break-word;white-space: pre-wrap;">{"token": " ... "}</pre>

How can I extract the token from this innerHTML object?

This is within a child browser of my Cordova application, which is why I am resorting to such an unconventional method.

Answer №1

To retrieve data, simply employ JSON.parse in combination with querySelector and innerHTML

let data = JSON.parse(document.querySelector('p').innerHTML);

The reason I utilize innerHTML is due to potential cross-browser inconsistencies with textContent or innerText

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

Validating and submitting forms using Bootstrap 4

I recently integrated a Bootstrap 4 form with validation using the provided "starter" JavaScript code available on their official website: https://getbootstrap.com/docs/4.4/components/forms/#validation. This form is located within a sidebar of a Google Do ...

How to use Javascript to set focus on a dropdown list within a PHP script

Having trouble setting focus on the dropdown list in my PHP page dc-test.php, where there are two dropdown lists that are interdependent. The values for these dropdown lists are fetched from a database table. I am unable to set focus on the first dropdown ...

Connect main data to sub-component

Example Vue Structure: <Root> <App> <component> Main function in main.js: function() { axios.get('/app-api/call').then(function (resp, error) { _this.response = resp.data; }) ...

Postpone the execution of the page controller until all necessary initial data has been collected

My Single Page Application (SPA) is in need of some initial data fetched from the server, such as user login status and permissions. However, I want to defer loading the page until this vital information is retrieved to avoid any issues with incomplete dat ...

End: unrelated, lacking responses

I have a question I'd like to discuss: Whenever I utilize the following code snippet: async function createNewElement(req,res){ const start1 = new Date().getTime(); console.log("start time 1 ", start1) await new Promise((resolve) => setT ...

"We are unable to set a value for a global array unless we utilize the .push() method

It's puzzling that I can't populate a global array without using the .push() method. (function() { var globalEmail = []; var testUpdate = function() { var arr = [1, 2, 3]; if (globalEmail.length > 1) { gl ...

Retrieving specific value from a Parent Controller in AngularJS using UI Router

I have a request to display the value stored in $scope.resAVal on my index.html page. This value is accessible within the RootCtrl. index.html <!DOCTYPE html> <html ng-app="plunker"> <head> <!-- any required JavaScript librarie ...

What is the method for retrieving the fixed information?

I am currently working on a project that involves creating a course-rating API. We have been given a completed angular application to work with, and our task is to set up the routes without making any changes to the Angular app. One of the initial tasks a ...

Unable to showcase my database data in array format

Thank you to everyone for your responses. Even though I've made some changes, I'm still unable to display my array. Here is what I have tried so far: $program = array(); $i = 0; while($row = $get_programmation->fetch_assoc()){ $progra ...

Filtering fields in Yii2 when using the find()->all() method: A guide

I need to generate a JSON object with only the id and name fields extracted from a database table. To achieve this, I have implemented a function within my AnalysisController file in my Yii2 application: public function actionAnalysis() { Yii::$app-& ...

What is the proper way to search for a specific string within a JavaScript array during an iteration?

I am working with an array that is continuously updated with new string elements every 2 seconds. The code snippet below showcases how the updates are processed: //tick world setInterval(function(){ doTradeUpdate(); },5000); function doTradeUpdate(){ ...

I am having trouble retrieving the array value from the response sent by my server

After receiving a dictionary from my server, when I try to access the values using the following code: {"filters":{ "Facture": [ "Магма (Тычок)", "Тонкий кирпич", "Гладк ...

Creating JOIN tables within the create action involves assigning related ids to each table

I am currently working on a room reservation system that involves including options for each room. Data related to the options and their join table, reservation_options, are successfully inserted into the params. However, I am facing an issue with assignin ...

Obtain the names of the cities that the Google Maps route passes through

Currently working on a website that integrates Google Map API v3. Is there a method to gather the names of the places or cities along the route from point A to B? The page utilizes JavaScript for functionality. ...

The concatenation function in JavaScript does not seem to be functioning properly with JSON

My attempt to use .concat() in order to combine two objects is resulting in tiles.concat is not a function The following code (in an angular app and written in coffeescript): $scope.tiles = new UI(); $scope.tiles.loadUITiles(); console.log($sco ...

Is it necessary to always provide a return value from a redux middleware function?

After reviewing the various examples provided in the redux documentation, it seems like there is always a return value from the middlewares. However, in my experience, simply calling next(action) and not returning anything seems to work without any issues. ...

What is the reason for JSON.parse throwing errors on the same strings that eval does not?

Imagine having something like this: var a = '["\t"]' When you use: eval('var result = ' + a) Everything runs smoothly. However, if you try: var result = JSON.parse(a) You'll encounter an error: Unexpected token. The s ...

What is the process for removing a property from the prototype within an array of MongoDB objects?

I have a database in MongoDB/Mongoose where I store user information, including passwords. However, when I want to display a list of contacts on the frontend, I don't want to include the passwords for security reasons. To achieve this, I attempted to ...

Adding the parsed JSON data into an HTML document

I've tried numerous methods, including tutorials and other online queries, but I can't seem to make it work. My goal is to transfer data from PHP to div elements and variables using jQuery. The array setup on the PHP side functions properly; howe ...

Generating support request with file attachment on Freshdesk

To generate a ticket with an attachment in Freshdesk API, I have successfully created one without any attachments. Here's a snippet of my code: post_dict = { 'helpdesk_ticket': { 'description': "Testing Code sample 3", ...