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

What is the meaning of this CSS acronym?

div[id^=picture]:target{ /*applying various styles here*/ } I stumbled upon the snippet of code shown above on a website discussing CSS image galleries. Can anyone clarify what this code accomplishes? Appreciate it. ...

"Utilizing long-polling techniques for cross-subdomain AJAX requests

Currently, I am developing a notifications script that continuously checks a database for any updates and displays them in a custom JavaScript popup. I have successfully implemented the jQuery AJAX loading and processing script as well as the PHP long pol ...

Displaying two images side by side in HTML using Bootstrap

As a beginner in HTML, I am faced with the challenge of placing two images of different sizes side by side on the same row using HTML and Bootstrap. Below is my current code: <div class="row"> <div class="col-lg-6 col-md-6 col-xs-6 col-sm-6"> ...

Is it possible to transfer files using web-bluetooth technology?

As I work on developing an embedded system that counts the number of cars, saves their speed and time data in a logs file using rsyslog. Simultaneously, I am creating a web-API (in Typescript/Angular with Electron for Desktop usage and later Web as well) t ...

Tips for testing a custom prompt that mandates a user to choose an option in the Bot Framework

Custom prompts using adaptive cards are essential for user inputs like input.ChoiceSet and input.Text. My challenge lies in testing the selection result when a card with 3 choices ('Red', 'Blue', 'Green') is presented. Specif ...

Which approach is more impactful: consistently sending an ajax request or breaking down the result within a function?

My JSON data consists of news entries with titles, text, and dates. Here is an example: { "news": [ {"title": "some title #1","text": "text","date": "27.12.15 23:45"}, {"title": "some title #2","text": "text","date": "26.12.15 22:35"}, ... ...

Converting Uniquely Designed Photoshop Shapes into HTML

I am working on a project to design an HTML page that features a profile picture with a heart frame. I have the PSD file with the heart frame included. The profile pictures will be rectangular in shape and should fit behind the heart frame without overlap ...

What is preventing me from using memoization in the getServerSideProps of NextJS?

I'm currently using React along with NextJS to showcase a variety of products on a specific category page. While I am able to successfully fetch the products utilizing getServerSideProps, I am not fond of how it continuously requests the product cata ...

Customizing React components based on API data

const LinkList = () => { const [links, setLinks] = useState([]); const url = 'http://localhost:5000/xyz'; const hook = () => { console.log('effect'); axios .get(url) .then(respo ...

Trouble with triggering HTML5 FileReader() functions

I'm having trouble determining why neither readSuccess() nor readFailure() are being executed in the code snippet below: function readMyFile(){ var reader = new FileReader(); reader.onload = readSuccess; reader.onerror = readFailure; ...

Customized settings saved in local storage using JavaScript

Here is the script I am currently using for my app: <script> if (localStorage.getItem("0") === null) { //do nothing } else if(localStorage.getItem("1")===null{ } else if(localStorage.getItem("2")===null{ } else if(localStorage.getItem("3")===null ...

Is there a way to determine if a user has the ability to navigate forward in Next.js?

I am faced with a challenge of determining whether a user can navigate forward on a webpage. My goal is to have two buttons - one to go back and another to go forward, with the forward button disabled when navigation is not possible. For the back button f ...

JS implementing a listener to modify a Google Map from a separate class

Currently, I am in the process of migrating my Google Map functionality from ionic-native to JavaScript. I am facing an issue while attempting to modify the click listener of my map from a separate class. The problem seems to be related to property errors. ...

Is there a way for me to access the user's gender and birthday following their login using their Google account details?

I have successfully implemented a Google sign-in button in my Angular application following the example provided in Display the Sign In With Google button: <div id="g_id_onload" class="mt-3" data-client_id="XXXXXXXXXXXX-XX ...

display the hidden box contents when clicking the button within a PHP loop

I am attempting to create a simple e-commerce site for learning purposes, but I encountered an issue when trying to display information upon clicking a button. The button does not seem to trigger any action as expected. It is meant to reveal text from the ...

Updating the node startup file with Visual Studio 2015 using NodeJS/Typescript

Encountering a persistent error: Error Code: TS5055 Cannot write file C:/project/dir/server.js' because it would overwrite the input file. Project: TypeScript/JavaScript Virtual Projects Even after renaming my entry filename to nodeserver.js, the ...

Utilizing headless Chrome to automatically capture AJAX requests

Chrome officially supports running the browser in headless mode, allowing for programmatic control through the Puppeteer API and/or the CRI library. I've thoroughly explored the documentation but have not discovered a method to programmatically captu ...

What is the best way to transfer my json file to the server?

How do I send a json file to the server using Flutter? { "likes":{ "job_name":"teh", "job_address":"teh" } , "equals":null } Upon trying to implement this, I enco ...

Using ThreeJS to Apply Dual Materials to a Mesh Entity

With ThreeJS, it's possible to incorporate more than one material into an Object3D/Mesh as stated in the documentation. You can either utilize a single Material or an array of Material: Class declaration and constructor for Mesh TypeScript file (exce ...

AngularJS directive error: Incorrect function invoked

I have two similar scenarios where I need to apply validators for specific files, even though I am aware that this goes against the DRY principle. However, being new to AngularJS, I am still learning the ropes. module.js var $moduleExample = angular.modu ...