Executing Javascript code from a specified web address

Let's say I have an image that needs to be shifted vertically, and I achieve it using the following code snippet:

document.getElementById('ImgID1').style.verticalAlign = 

However, the value by which I need to shift the image is provided through a URL (the URL is given below for reference), where if I were to run the contents of the URL as JavaScript, the output would be "-8px", which is exactly what I need. But how can I implement this? How do I express

verticalAlign={retrieve the URL content, execute as JS, extract the returned value}
.

The specific URL in question is http://latex.codecogs.com/gif.json?\inline%20\mathbb{R}

Here is a sample response that may shed some light on how to approach this:

ParseEqn({ "latex": { "type":"gif", "equation":"\\mathbb{R}", "site":"stackoverflow.com", "file":"f3ed131812d10a06a9349ab2b42e3ed4.gif", "url":"http://www.codecogs.com/eq/f3/f3ed131812d10a06a9349ab2b42e3ed4.gif", "width":"12", "height":"12", "baseline":"1" } });

Your insights would be greatly appreciated!

Sincerely,
Sam

Answer №1

Check out the code snippet below to receive a response from the server. Make sure to open the console to confirm.

JSFIDDLE : Using JQUERY

window.ParseEqn = function(data) {
alert(data.latex.type);
}

$.ajax({
    url: 'http://latex.codecogs.com/gif.json?%5Cinline%20%5Cmathbb%7BR%7D',
    crossDomain: true,

    contentType: "application/json; charset=utf-8",
    dataType: 'jsonp',
    headers: {"Access-Control-Allow-Origin": '*'
             },  
})

Using pure JavaScript: suggested solution by Blue skies

window.ParseEqn = function(data) {
    alert(data.latex.width);
}

var s = document.createElement("script")
s.src = "http://latex.codecogs.com/gif.json?%5Cinline%20%5Cmathbb%7BR%7D";
s.onload = function() {
    document.body.removeChild(s);
};
document.body.appendChild(s);

Answer №2

If you want to fetch a JSON file using AJAX, you can do that by following these steps:

$.getJSON('gif.json', function(data) {

However, keep in mind that simply specifying the URL in the function may not automatically trigger the download and processing.

var image = document.createElement("IMG")

Add the image to your webpage like this:

document.body.appendChild(image); 
    $.getJSON('<a href="http://latex.codecogs.com/gif.json" rel="nofollow">http://latex.codecogs.com/gif.json</a>?\inline%20\mathbb{R}', function(data) {
          var items = [];
      $.each(data, function(key, val) {
        if(key=="url")
        image.src=value;
        else if (key="width")
        image.style.width=value;
        else if (key="heigth")
        image.style.heigth=value;
              });
    document.getElementById("yourdive's case").appendChild(image);
    });

Hopefully, this is what you were looking for!

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

Submitting the Ajax form will result in the quantity of the product in the cart being updated while remaining on

Hello, I am using ajax to keep the user on the same page after submitting a form. If successful, I will use ajax load to load another page that displays the quantity of the product. However, I am facing an issue where each subsequent submit increases the q ...

What is the best way to insert a React component or raw HTML into another React component?

Dealing with raw HTML markup returned from an AJAX call can be tricky in React. I've tried using dangerouslySetInnerHTML, but React just throws errors when I do. It's like trying to navigate through a maze. After some trial and error, I decided ...

NodeJS Exporting Features

A situation is in front of me: var express = require('express'); var router = express.Router(); var articles = require('../model/articles.js'); router.get('/all', function(req, res, next) { res.json(articles.getAll()); ...

Email replay feature

Having an issue with my email validation function. I found the code I'm using here: Repeat email in HTML Form not the same. Why? The problem I am facing is that if you incorrectly enter your email in the first input "eMail" and correctly in the seco ...

Issue encountered while creating a token in a JavaScript Chrome extension and attempting to validate it on a backend Node.js server

Trying to create a token within a chrome extension and utilizing it to authenticate requests to the backend server has proven challenging. While successfully generating a token on the front end, encountering an error when verifying it with the google-auth- ...

Aligning a row with space between columns

I am looking to display 6 divs on my website with a margin between them for aesthetics. In order to maintain the design when resizing the site, I had to specify widths for the columns. However, I am struggling to center all the cols effectively, despite tr ...

Accept either a string or an integer in the JSON response

When dealing with a dictionary fetched from a rest API call, the catData variable holds important information: NSString* catId = catData[@"id"]; This approach works well if catData["id"] is a string. However, it may not be suitable if it's an intege ...

The issue of Basic Bootstrap 5 Modal triggering twice is causing a frustrating experience

My modal is almost working perfectly - it changes content based on the clicked image, but it is triggering twice in the backend and I can't figure out why! I followed Bootstrap's documentation for this, so I'm unsure where the issue lies. Al ...

Ajax response data triggered twice

I'm struggling to understand why my data is being called twice. I attempted to replace 'append', but it's not working. I suspect the issue lies within my controller. Here is my Ajax call: jQuery(document).ready(function($) { $('# ...

Workaround for syncing MobX observables when toggling a select/deselect all checkbox

In my application, there is a checkbox list with a 'Select All' checkbox at the top. The list is populated by an observable array of strings. When the user clicks on 'Select All', it should check all checkboxes; subsequent clicks should ...

Converting XML data into a JSON format that is compatible with loading into BigQuery

While learning Python at work, I have managed to successfully load XML data into BigQuery, but I'm not entirely confident in my approach. My process involves calling an API that returns an XML structure, which I parse using ElementTree and tree.iter( ...

How can I update the value of a span element that was added to the HTML document through an AJAX request?

When a modal is triggered on click, data is added to it through an AJAX request to a PHP file. After the AJAX call, I want the values in span elements within the modal to change simultaneously with the input field of the modal. I attempted this JavaScript ...

Creating a fresh CSS class and utilizing its properties in JavaScript is the task at hand

Whenever I define a css class and attempt to access its member from JavaScript, the result always ends up being undefined. Where could my mistake possibly lie? function myFunction() { var x = document.getElementById("myId").style.myMember; document. ...

Positioning elements precisely on a webpage through absolute positioning and floating them

This seems to present a conflicting situation. Ideally, I am looking to position two child elements on the left and right edges of the parent element, vertically centered, and overlaying all other sibling elements. ...

changing tooltip color in bootstrap based on the parent element

Is there a way to adjust the bootstrap tooltip background-color based on the container? For example, my header has a white background and my footer is black. #header .tooltip-inner{ background-color:fade(@grayWhite,80); } #footer .tooltip-inner{ backgro ...

Align the responsive image in the center of a container

I am facing a challenge in centering an image wrapped by an anchor tag inside a div. I have tried using display flexbox and justify-content:center for the image, which works to some extent. However, the height of the image remains constant while the width ...

Pair a specific portion of text with another string

I'm having trouble finding a substring within a string. The substring and the string are as follows: var str="My name is foo.I have bag(s)" var substr="I have bag(s)" When I use str.match(substr), it returns null, probably because the match() funct ...

Tips for managing API lists and authorization keys within the appsettings file

Currently, I have coded an API call in my ASP.NET Core MVC application with the authorization key embedded directly within the controller. How can I securely store this API list and authorization key in the appsetting.json file for easier configuration c ...

A critical issue occurred: array length is invalid. The attempt to include EJS in the template failed due to

Currently, I am attempting to loop through my JSON data using EJS from Node/Express. However, I need to insert a different pin into the flexbox when it reaches the 6th iteration. Whenever I try to implement this logic, I encounter a severe error that disr ...

I am looking to display the results table on the same page after submitting a form to filter content. Can you provide guidance on how to achieve this?

Could someone provide guidance on how to approach the coding aspect of my current issue? I have a search form that includes a select form and a text box. Upon submission, a table is generated with results filtered from the form. Should I utilize a sessio ...