Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

170
Views
Trying to make the button onclick access an API Url link

Im creating a news app project that pulls JSON data from an API.

The API library provides a link to the "full story."

How do I change the address of window.open() when the url is essentially an array location within a JSON file?

Heres my solution thus far

HTML

 <button onclick="newWindow()">Click for full story</button>

JavaScript

 function newWindow(){
  window.open(data.news[0].url)
   }

JSON

{
  "status": "ok",
  "news": [
    {
      "id": "4e6",
      "title": "Soccer team wins the World Cup"
      "url": "wwww.newsnetwork.com/soccer-team-wins-the-world-cup"
...
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can use the javascript's built in function fetch, to get what your api is sending back. After that you have to make it into a object(JSON) to access it's properties.

Note: Don't forget to handle errors in case the api isn't working or you url is missing, you can do all that in the .catch

function newWindow() {
        fetch("http://example.com/movies.json")
        .then(response => response.json())
        .then((data) => {
            window.open(data.news[0].url);
        })
        .catch((error) => console.error("oops:",error));
    }
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!