Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

175
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda