Trigger window.open as ajax complete











up vote
1
down vote

favorite












In a website there are a some jQuery ajax calls, in some of them, if success, is triggered a winwdow.open in another domain. I own the other domain sites.



For example:




  1. site https://site1.example.com make on ajax call on itself

  2. if success trigger a window.open on https://site1.example2.com or https://site2.example.com

  3. often the popup blocker blocks the operation


How can I handle this issue? Do I have to enable CORS on my site/server https://enable-cors.org/ ?



Code (sure, I don't own google):



$.ajax({
url:'/echo/js/?js=hello%20world!',
complete: function (response) {
$('#output').html(response.responseText);
window.open('https://www.google.com');
},
error: function () {
$('#output').html('Bummer: there was an error!');
},
});


Demo: https://jsfiddle.net/IrvinDominin/nbsu3hgk/










share|improve this question






















  • When I allow pop-ups in the browser this always works. Tested with Firefox and Chromium.
    – D. Joe
    2 days ago










  • I know, but, if possible I have to handle this without browser options or it produce some help desk on my users
    – Irvin Dominin
    2 days ago










  • Have a look here: stackoverflow.com/a/2587692/2412335
    – D. Joe
    2 days ago















up vote
1
down vote

favorite












In a website there are a some jQuery ajax calls, in some of them, if success, is triggered a winwdow.open in another domain. I own the other domain sites.



For example:




  1. site https://site1.example.com make on ajax call on itself

  2. if success trigger a window.open on https://site1.example2.com or https://site2.example.com

  3. often the popup blocker blocks the operation


How can I handle this issue? Do I have to enable CORS on my site/server https://enable-cors.org/ ?



Code (sure, I don't own google):



$.ajax({
url:'/echo/js/?js=hello%20world!',
complete: function (response) {
$('#output').html(response.responseText);
window.open('https://www.google.com');
},
error: function () {
$('#output').html('Bummer: there was an error!');
},
});


Demo: https://jsfiddle.net/IrvinDominin/nbsu3hgk/










share|improve this question






















  • When I allow pop-ups in the browser this always works. Tested with Firefox and Chromium.
    – D. Joe
    2 days ago










  • I know, but, if possible I have to handle this without browser options or it produce some help desk on my users
    – Irvin Dominin
    2 days ago










  • Have a look here: stackoverflow.com/a/2587692/2412335
    – D. Joe
    2 days ago













up vote
1
down vote

favorite









up vote
1
down vote

favorite











In a website there are a some jQuery ajax calls, in some of them, if success, is triggered a winwdow.open in another domain. I own the other domain sites.



For example:




  1. site https://site1.example.com make on ajax call on itself

  2. if success trigger a window.open on https://site1.example2.com or https://site2.example.com

  3. often the popup blocker blocks the operation


How can I handle this issue? Do I have to enable CORS on my site/server https://enable-cors.org/ ?



Code (sure, I don't own google):



$.ajax({
url:'/echo/js/?js=hello%20world!',
complete: function (response) {
$('#output').html(response.responseText);
window.open('https://www.google.com');
},
error: function () {
$('#output').html('Bummer: there was an error!');
},
});


Demo: https://jsfiddle.net/IrvinDominin/nbsu3hgk/










share|improve this question













In a website there are a some jQuery ajax calls, in some of them, if success, is triggered a winwdow.open in another domain. I own the other domain sites.



For example:




  1. site https://site1.example.com make on ajax call on itself

  2. if success trigger a window.open on https://site1.example2.com or https://site2.example.com

  3. often the popup blocker blocks the operation


How can I handle this issue? Do I have to enable CORS on my site/server https://enable-cors.org/ ?



Code (sure, I don't own google):



$.ajax({
url:'/echo/js/?js=hello%20world!',
complete: function (response) {
$('#output').html(response.responseText);
window.open('https://www.google.com');
},
error: function () {
$('#output').html('Bummer: there was an error!');
},
});


Demo: https://jsfiddle.net/IrvinDominin/nbsu3hgk/







javascript cors window.open






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked 2 days ago









Irvin Dominin

27.3k85490




27.3k85490












  • When I allow pop-ups in the browser this always works. Tested with Firefox and Chromium.
    – D. Joe
    2 days ago










  • I know, but, if possible I have to handle this without browser options or it produce some help desk on my users
    – Irvin Dominin
    2 days ago










  • Have a look here: stackoverflow.com/a/2587692/2412335
    – D. Joe
    2 days ago


















  • When I allow pop-ups in the browser this always works. Tested with Firefox and Chromium.
    – D. Joe
    2 days ago










  • I know, but, if possible I have to handle this without browser options or it produce some help desk on my users
    – Irvin Dominin
    2 days ago










  • Have a look here: stackoverflow.com/a/2587692/2412335
    – D. Joe
    2 days ago
















When I allow pop-ups in the browser this always works. Tested with Firefox and Chromium.
– D. Joe
2 days ago




When I allow pop-ups in the browser this always works. Tested with Firefox and Chromium.
– D. Joe
2 days ago












I know, but, if possible I have to handle this without browser options or it produce some help desk on my users
– Irvin Dominin
2 days ago




I know, but, if possible I have to handle this without browser options or it produce some help desk on my users
– Irvin Dominin
2 days ago












Have a look here: stackoverflow.com/a/2587692/2412335
– D. Joe
2 days ago




Have a look here: stackoverflow.com/a/2587692/2412335
– D. Joe
2 days ago












1 Answer
1






active

oldest

votes

















up vote
0
down vote













Because window.open requires direct user action (e. g. a click) to avoid popup-blockers, you could change your code like this:



$.ajax({
url:'/echo/js/?js=hello%20world!',
complete: function (response) {
$('#output').html(response.responseText);
//window.open('https://www.google.com');
$( "body" ).append('<button id="openPopup">Open Site</button>');
$( "#openPopup" ).click( function() {
window.open('https://www.google.com');
});
},
error: function () {
$('#output').html('Bummer: there was an error!');
},
});


If the ajax request is successful, users are shown a button which they have to click to get to the other domain.



Edit: Referring to your comment below I think I found a solution:



var myPopup;
$( document ).on("click", "#myButton", function() {
myPopup = window.open("/");

$.ajax({
url:'/echo/js/?js=hello%20world!',
complete: function (response) {
$('#output').html(response.responseText);
//window.open('https://www.google.com');
myPopup.location.href = 'https://www.google.com';
},
error: function () {
$('#output').html('Bummer: there was an error!');
},
});
});


You have to declare a global variable for your new window first. When your button that triggers the ajax request is clicked you open a new window that is referencing to your current domain (https://site1.example.com/ in your case). This is accepted as direct user action, so no popup blocker comes into effect.



In the complete function you redirect that window to one of the other domains (https://site1.example2.com or https://site2.example.com), et voilà. I've tested it with activated popup blocker and it worked.






share|improve this answer























  • Is triggered by a click on a button, that fires the Ajax call
    – Irvin Dominin
    2 days ago










  • I guess that the click on that button is not considered a direct user action by browsers for thewindow.open in the 'complete` function of the ajax request. The code above at least works.
    – D. Joe
    2 days ago












  • I've edited my code above to suit your needs.
    – D. Joe
    2 days ago











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53238309%2ftrigger-window-open-as-ajax-complete%23new-answer', 'question_page');
}
);

Post as a guest
































1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote













Because window.open requires direct user action (e. g. a click) to avoid popup-blockers, you could change your code like this:



$.ajax({
url:'/echo/js/?js=hello%20world!',
complete: function (response) {
$('#output').html(response.responseText);
//window.open('https://www.google.com');
$( "body" ).append('<button id="openPopup">Open Site</button>');
$( "#openPopup" ).click( function() {
window.open('https://www.google.com');
});
},
error: function () {
$('#output').html('Bummer: there was an error!');
},
});


If the ajax request is successful, users are shown a button which they have to click to get to the other domain.



Edit: Referring to your comment below I think I found a solution:



var myPopup;
$( document ).on("click", "#myButton", function() {
myPopup = window.open("/");

$.ajax({
url:'/echo/js/?js=hello%20world!',
complete: function (response) {
$('#output').html(response.responseText);
//window.open('https://www.google.com');
myPopup.location.href = 'https://www.google.com';
},
error: function () {
$('#output').html('Bummer: there was an error!');
},
});
});


You have to declare a global variable for your new window first. When your button that triggers the ajax request is clicked you open a new window that is referencing to your current domain (https://site1.example.com/ in your case). This is accepted as direct user action, so no popup blocker comes into effect.



In the complete function you redirect that window to one of the other domains (https://site1.example2.com or https://site2.example.com), et voilà. I've tested it with activated popup blocker and it worked.






share|improve this answer























  • Is triggered by a click on a button, that fires the Ajax call
    – Irvin Dominin
    2 days ago










  • I guess that the click on that button is not considered a direct user action by browsers for thewindow.open in the 'complete` function of the ajax request. The code above at least works.
    – D. Joe
    2 days ago












  • I've edited my code above to suit your needs.
    – D. Joe
    2 days ago















up vote
0
down vote













Because window.open requires direct user action (e. g. a click) to avoid popup-blockers, you could change your code like this:



$.ajax({
url:'/echo/js/?js=hello%20world!',
complete: function (response) {
$('#output').html(response.responseText);
//window.open('https://www.google.com');
$( "body" ).append('<button id="openPopup">Open Site</button>');
$( "#openPopup" ).click( function() {
window.open('https://www.google.com');
});
},
error: function () {
$('#output').html('Bummer: there was an error!');
},
});


If the ajax request is successful, users are shown a button which they have to click to get to the other domain.



Edit: Referring to your comment below I think I found a solution:



var myPopup;
$( document ).on("click", "#myButton", function() {
myPopup = window.open("/");

$.ajax({
url:'/echo/js/?js=hello%20world!',
complete: function (response) {
$('#output').html(response.responseText);
//window.open('https://www.google.com');
myPopup.location.href = 'https://www.google.com';
},
error: function () {
$('#output').html('Bummer: there was an error!');
},
});
});


You have to declare a global variable for your new window first. When your button that triggers the ajax request is clicked you open a new window that is referencing to your current domain (https://site1.example.com/ in your case). This is accepted as direct user action, so no popup blocker comes into effect.



In the complete function you redirect that window to one of the other domains (https://site1.example2.com or https://site2.example.com), et voilà. I've tested it with activated popup blocker and it worked.






share|improve this answer























  • Is triggered by a click on a button, that fires the Ajax call
    – Irvin Dominin
    2 days ago










  • I guess that the click on that button is not considered a direct user action by browsers for thewindow.open in the 'complete` function of the ajax request. The code above at least works.
    – D. Joe
    2 days ago












  • I've edited my code above to suit your needs.
    – D. Joe
    2 days ago













up vote
0
down vote










up vote
0
down vote









Because window.open requires direct user action (e. g. a click) to avoid popup-blockers, you could change your code like this:



$.ajax({
url:'/echo/js/?js=hello%20world!',
complete: function (response) {
$('#output').html(response.responseText);
//window.open('https://www.google.com');
$( "body" ).append('<button id="openPopup">Open Site</button>');
$( "#openPopup" ).click( function() {
window.open('https://www.google.com');
});
},
error: function () {
$('#output').html('Bummer: there was an error!');
},
});


If the ajax request is successful, users are shown a button which they have to click to get to the other domain.



Edit: Referring to your comment below I think I found a solution:



var myPopup;
$( document ).on("click", "#myButton", function() {
myPopup = window.open("/");

$.ajax({
url:'/echo/js/?js=hello%20world!',
complete: function (response) {
$('#output').html(response.responseText);
//window.open('https://www.google.com');
myPopup.location.href = 'https://www.google.com';
},
error: function () {
$('#output').html('Bummer: there was an error!');
},
});
});


You have to declare a global variable for your new window first. When your button that triggers the ajax request is clicked you open a new window that is referencing to your current domain (https://site1.example.com/ in your case). This is accepted as direct user action, so no popup blocker comes into effect.



In the complete function you redirect that window to one of the other domains (https://site1.example2.com or https://site2.example.com), et voilà. I've tested it with activated popup blocker and it worked.






share|improve this answer














Because window.open requires direct user action (e. g. a click) to avoid popup-blockers, you could change your code like this:



$.ajax({
url:'/echo/js/?js=hello%20world!',
complete: function (response) {
$('#output').html(response.responseText);
//window.open('https://www.google.com');
$( "body" ).append('<button id="openPopup">Open Site</button>');
$( "#openPopup" ).click( function() {
window.open('https://www.google.com');
});
},
error: function () {
$('#output').html('Bummer: there was an error!');
},
});


If the ajax request is successful, users are shown a button which they have to click to get to the other domain.



Edit: Referring to your comment below I think I found a solution:



var myPopup;
$( document ).on("click", "#myButton", function() {
myPopup = window.open("/");

$.ajax({
url:'/echo/js/?js=hello%20world!',
complete: function (response) {
$('#output').html(response.responseText);
//window.open('https://www.google.com');
myPopup.location.href = 'https://www.google.com';
},
error: function () {
$('#output').html('Bummer: there was an error!');
},
});
});


You have to declare a global variable for your new window first. When your button that triggers the ajax request is clicked you open a new window that is referencing to your current domain (https://site1.example.com/ in your case). This is accepted as direct user action, so no popup blocker comes into effect.



In the complete function you redirect that window to one of the other domains (https://site1.example2.com or https://site2.example.com), et voilà. I've tested it with activated popup blocker and it worked.







share|improve this answer














share|improve this answer



share|improve this answer








edited 2 days ago

























answered 2 days ago









D. Joe

12910




12910












  • Is triggered by a click on a button, that fires the Ajax call
    – Irvin Dominin
    2 days ago










  • I guess that the click on that button is not considered a direct user action by browsers for thewindow.open in the 'complete` function of the ajax request. The code above at least works.
    – D. Joe
    2 days ago












  • I've edited my code above to suit your needs.
    – D. Joe
    2 days ago


















  • Is triggered by a click on a button, that fires the Ajax call
    – Irvin Dominin
    2 days ago










  • I guess that the click on that button is not considered a direct user action by browsers for thewindow.open in the 'complete` function of the ajax request. The code above at least works.
    – D. Joe
    2 days ago












  • I've edited my code above to suit your needs.
    – D. Joe
    2 days ago
















Is triggered by a click on a button, that fires the Ajax call
– Irvin Dominin
2 days ago




Is triggered by a click on a button, that fires the Ajax call
– Irvin Dominin
2 days ago












I guess that the click on that button is not considered a direct user action by browsers for thewindow.open in the 'complete` function of the ajax request. The code above at least works.
– D. Joe
2 days ago






I guess that the click on that button is not considered a direct user action by browsers for thewindow.open in the 'complete` function of the ajax request. The code above at least works.
– D. Joe
2 days ago














I've edited my code above to suit your needs.
– D. Joe
2 days ago




I've edited my code above to suit your needs.
– D. Joe
2 days ago


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53238309%2ftrigger-window-open-as-ajax-complete%23new-answer', 'question_page');
}
);

Post as a guest




















































































Popular posts from this blog

Full-time equivalent

さくらももこ

13 indicted, 8 arrested in Calif. drug cartel investigation