window.open() does not load the page

Asked

Viewed 394 times

4

I am trying to open a link in a new window/tab but when it opens it does not load.

Controller:

public static Result loadCreateArtigo(){
     return ok(request().host()+request().path());
}

Javascript:

$.SmartMessageBox({
  buttons : '[Não][Sim]'
}, 
function(ButtonPressed) {
  if (ButtonPressed === "Sim") {            
    Controller.loadCreateArtigo().ajax({
      success: function(data){
          window.open(data);
      },                   
  });
});

The data variable returns the url. What happens is that it opens a new tab with the right url but the page does not load. if F5 gives the page right click. I’m using Google Chrome. Any suggestions?

  • Dude, try passing the parameter "_blank" along -> window.open(data, "_blank");

  • 2

    I have tried this but it does not work. I have found the solution. window.open() must receive an absolute url, so if the host received on the date does not contain http[s], it is necessary to add it: window.open("http://"+data);

1 answer

1

Hello,

as mentioned by our friend @Hatchet-Knife, it is necessary to include

'http:// ou https://'

in the URL, in your case on the date.

to test, you can:

//verificar se existe http ou https
var data = 'http://asdasd' 
if(data.search(/https?\:\/\//ig) < 0){
    //caso não tenha, coloca pelo menos um http://
    data = 'http://'+data
    alert(data);
}

the search uses regex, as explained here

hope I’ve helped

Browser other questions tagged

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