window.open is not opening the popup

Asked

Viewed 781 times

2

I have an extension that sends things to an external server by ajax post (all in javascript) and the reply of the send (sent from the server) is received in the extension itself. I want to put this answer in another pop-up. How can I do that? I tried it with window.open(...,'_self',...) but it didn’t, it doesn’t open the window. Do you have any help there? BS: I don’t know how to use jquery

  • 2

    Could you add the code that makes the POST ajax? So it’s easier to see what you’re doing.

1 answer

1

I made an extension example that allows you to do what you asked.

In the archive manifest.json it is necessary to add a permission to have access to the host, otherwise it will not be possible to execute ajax. In this case, I will use an echo page jsfiddle:

"permissions": [
    "http://jsfiddle.net/"   // aqui está a permissão necessária, para acessar o host
]

The rest is just like normal Web development... you can even use jQuery. I put the jquery file together with the extension files. The version used is the one that is linked in the file listing below.

Archives

  • jquery-1.11.2.min. js

  • manifest.json

    {
      "manifest_version": 2,
    
      "name": "Extensão de teste, com Ajax e Janela de Pop-up",
      "description": "Extensão de teste, com Ajax e Janela de Pop-up",
      "version": "1.0",
    
      "browser_action": {
        "default_popup": "popup.html"
      },
    
      "permissions": [
        "http://jsfiddle.net/"
      ]
    }
    
  • popup.html

    <!doctype html>
    <html>
      <head>
        <title>Abrindo resultado de ajax em outra tela</title>
        <script src="jquery-1.11.2.min.js"></script>
        <script src="popup.js"></script>
      </head>
      <body>
        <button id="btn">Abrir conteúdo em outra janela</button>
      </body>
    </html>
    
  • popup.js

    $(function() {
        $('#btn').click(function() {
                $.ajax({
                    url: 'http://jsfiddle.net/echo/html/',
                    data: {
                        html: '<div>Documento carregado via ajax!</div>',
                        delay: 1
                    },
                    type: 'POST',
                    success: function(data){
                        var win = window.open(
                            "",
                            "Title",
                            "toolbar=no,"+
                            "location=no,"+
                            "directories=no,"+
                            "status=no,"+
                            "menubar=no,"+
                            "scrollbars=yes,"+
                            "resizable=yes,"+
                            "width=780,"+
                            "height=200,"+
                            "top="+(screen.height-400)+","+
                            "left="+(screen.width-840));
                        win.document.body.innerHTML = data;
                    },
                    error: function() {
                        alert("error: "+JSON.stringify(arguments));
                    }
                });
                return false;
            });
    });
    

Browser other questions tagged

You are not signed in. Login or sign up in order to post.