Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

171
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda