The standard popup-blocker logic contained in most browsers these days will block any calls to window.open() that are not the direct result of a user action. Code that is triggered by timers or by any asynchronous callback (like your ajax ready function) will be treated as NOT caused directly by user actions and the new popup window will generally be blocked.
You can verify this is what is happening by temporarily changing your browser's popup blocking (turning it off) and see that it then starts working.
Probably what you need to do as a work-around is to create the window upon the user action that started this thread of code and then put the content into the window when you get your ajax response. The browser will probably allow that. I know that's less desirable from a visual perspective, but you can put some temporary content in the window until the ajax response comes in (something like "loading...").
Answer from jfriend00 on Stack OverflowThe standard popup-blocker logic contained in most browsers these days will block any calls to window.open() that are not the direct result of a user action. Code that is triggered by timers or by any asynchronous callback (like your ajax ready function) will be treated as NOT caused directly by user actions and the new popup window will generally be blocked.
You can verify this is what is happening by temporarily changing your browser's popup blocking (turning it off) and see that it then starts working.
Probably what you need to do as a work-around is to create the window upon the user action that started this thread of code and then put the content into the window when you get your ajax response. The browser will probably allow that. I know that's less desirable from a visual perspective, but you can put some temporary content in the window until the ajax response comes in (something like "loading...").
Just had this exact same issue. Just in case you wanted the code that fixed it. I used this:
newWindow = window.open("", "_blank");
request = $.ajax({ ... my request which returns a url to load ... })
request.done((function(_this) {
return function(data, textStatus, jqXHR) {
return newWindow.location = data.Url;
};
})(this));
window.open not opening window
Javascript Window Open not working
Window.open(); doesn't work properly | Vivaldi Forum
Javascript window.open not working
Second parameter must be the window name:
<input type="button" value="new win"
onclick="window.open('http://yahoo.com', 'mywindow', 'width=500, height=400')" />
Working fine in Chrome and Firefox:
http://jsfiddle.net/DvMy5/2/
The second parameter should be name..Something like windowname
<input type="button" value="new win"
onclick="window.open('http://yahoo.com','windowname', 'width=500, height=400')" />
I have set a password on my site and im trying to get it working completly. i have the whole login system but it is writen in an javascript. but i got one problem. if the password is guessed correct it should open my main.html. but it won't open.
This is my code
window.open('main.html',"_self");
it wont open the main html if the target is _self. it will open when there is no target. it also will open https://www.google.com/ if i use :
windows.open( 'https://www.google.com/','_self');
Can anyone help me?
I am trying to create a temporary fix for this highly-annoying bug:
https://www.reddit.com/r/help/comments/13o7yif/why_does_reddit_open_image_links_in_their/
The solution to bust the proprietary image viewer is to open the image URL in a new tab and then close the current tab:
//Check to see if the image is being loaded in Reddit's proprietary image viewer. If the element exists, open the URL in a new tab and then close the current.
let elImg = document.querySelector("img.object-contain");
if (elImg) {
window.open(elImg.src,"_blank");
window.close();
}This works great when executed in the dev console directly, but Brave blocks it as a pop-up when it's executed by my Tampermonkey userscript. Is there a way to somehow trust the userscript source? Or perhaps there is another way to open a URL in a new tab without being blocked?
Update: While not an ideal solution, this does work:
let elImg = document.querySelector("img.object-contain");
if (elImg) {
elImg.addEventListener("click", function() {
window.open(this.src,"_blank");
window.close();
});
}It turns out that pop-ups are blocked when not executed by a user action. If I invoke the code by adding a click event to the element, it will work. However, I am still looking for a way to execute the code when the page loads.