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

267
Vistas
Javascript How to open a new tab?

I've created a function using getJson that allows a user to search for a specific user and display all their data.

 <body>

     <input id="search" type="text">
     <button>Search</button>
     <div id="results"></div>

     <script>
         $('button').on('click', function() {
             var user = $('#search').val();
             $.getJSON("https://api.github.com/users/" + user)
                 .done(function(user) {
                     var br = "<br>";
                     var p = $("<p id='users'></p>");
                     var name = "Username: " + user.login + br;
                     var pic = "Avatar Picture:" + br + "<img src='" + user.avatar_url + "'/>" + br;
                     var homeURl = "Homepage URL: " + "<a href='" + user.html_url + "'>" + user.html_url + "</a>" + br;
                     var location = "Location: " + "Null" + br;
                     var admin = "Admin: " + user.site_admin;
                     p.append("<p>" + name + pic + homeURl + location + admin + "</p>");

                     $("#results").empty().append(p);
                 })
                 .fail(function(jqXHR) {
                     console.log("Error: " + jqXHR.status);
                 })
                 .always(function() {
                     console.log("Random Users Request finished");
                 });
         });
     </script>
 </body>

For example if the user types in "mojombo" then all the information relevant to the user "mojombo" is displayed.

I am currently wishing to modify this function such this data is accessed through clicking on the user name rather than searching. For example, if the user name "mojombo" is displayed and the user clicks the name then a new tab should open displaying all the information relevant to the user mojombo.

How would I be able to accomplish this?

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

Edit: Based on your question specifically. You just need to add the target attribute to the a tag you already have.

 var homeURl = "Homepage URL: " + "<a href='" + user.html_url + "' target='_blank'>" + user.html_url + "</a>" + br;

Here's a snippet.

$('button').on('click', function () {
        var user = $('#search').val();
        $.getJSON("https://api.github.com/users/" + user)
          .done(function (user) {
            var br = "<br>";
            var p = $("<p id='users'></p>");
            var name = "Username: " + user.login + br;
            var pic = "Avatar Picture:" + br + "<img src='" + user.avatar_url + "'/>" + br;
            var homeURl = "Homepage URL: " + "<a href='" + user.html_url + "' target='_blank'>" + user.html_url + "</a>" + br;
            var location = "Location: " + "Null" + br;
            var admin = "Admin: " + user.site_admin;
            p.append("<p>" + name + pic + homeURl + location + admin + "</p>");

            $("#results").empty().append(p);
          })
          .fail(function (jqXHR) {
            console.log("Error: " + jqXHR.status);
          })
          .always(function () {
            console.log("Random Users Request finished");
          });
      });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<input id="search" type="text">
    <button>Search</button>
    <div id="results"></div>

about 4 years ago · Juan Pablo Isaza Denunciar

0

$('button').on('click', function(e) {
    window.open($(e.currentTarget).attr('href'));
}
about 4 years ago · Juan Pablo Isaza Denunciar

0

As far as I could understand your question

<!DOCTYPE html>
<html>

<head>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js" integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script>
</head>

<body>

    <input id="search" type="text">
    <button>Search</button>
    <div id="results"></div>

    <script>
        const html = $("<html></html>");
        $('button').on('click', function() {
            var user = $('#search').val();
            $.getJSON("https://api.github.com/users/" + user)
                .done(function(user) {
                    var br = "<br>";
                    var p = $("<p id='users'></p>");
                    var name = "Username: " + user.login + br;
                    var pic = "Avatar Picture:" + br + "<img src='" + user.avatar_url + "'/>" + br;
                    var homeURl = "Homepage URL: " + "<a href='" + user.html_url + "'>" + user.html_url + "</a>" + br;
                    var location = "Location: " + "Null" + br;
                    var admin = "Admin: " + user.site_admin;
                    p.append("<p>" + name + pic + homeURl + location + admin + "</p>");
                    html.append($('<body>').append(p));
                    var w = window.open("");
                    w.document.write(html.html())
                })
                .fail(function(jqXHR) {
                    console.log("Error: " + jqXHR.status);
                })
                .always(function() {
                    console.log("Random Users Request finished");
                });
        });
    </script>
</body>

</html>
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