Pausing execution of calling script when fancybox is open - javascript

I have implemented some form processing using jquery and in some circumstances more information is needed in which case I open a new page in fancybox as shown:
moreInfoNeeded = checkInfoComplete();
if (moreInfoNeeded) {
$.fancybox.open({
href : 'iframe.html',
type : iframe,
padding : 5
});
// We need to pause execution here until fancybox is closed.
}
// Once fancybox has been closed we have all the information so can continue.
$.ajax({
...
success: function(data)
{
window.location.replace(data);
}
});
How do I suspend the execution of the script whilst the fancybox window is open? At the moment the code above starts to open the fancybox window with the iframe in it but the redirect using window.location.replace occurs and closes the fancybox window in the process.
Thanks,
Andy.

Related

Close window with JavaScript background script? (Firefox Extension)

I am currently writing a Firefox extension that opens a new window in my background script. When a user clicks a button on a page the background script executes and opens the window.
So far I have:
// content script
downloadMod(linkToOpen); //button clicked
function downloadMod(url) {
// var test = window.open(url, '_blank');
var myPort = browser.runtime.connect({ name: "cac410c4672fff93bf0d3186636d8876de3dfeb6#temporary-addon"});
myPort.postMessage({ greeting: url });
}
In my background script (the script the above code connects too) I have:
//background script
portFromCS.onMessage.addListener(function (m) {
var test = browser.windows.create({
url: m.greeting,
allowScriptsToClose: true,
});
NOTE: all the code works as expected. However, the trouble comes when closing the window.
I have tried variations of self.close(), window.close(). I even created a function to wait 5 seconds to load the newly created window and then close the page to no avail. All of my attempts come back with the error:
Scripts may not close windows that were not opened by script.
I thought this error was supposed to be removed with the allowScriptsToClose flag?
To close a window you created with browser.windows.create, you need to use browser.windows.remove.
So:
let windowId;
browser.windows.create({url: "http://google.be"}).then((data) => {
windowId = data.id;
});
// Sometime later, use windowId to close the window
setTimeout(function(){
browser.windows.remove(windowId);
}, 5000);

Jquery Cookie : one time use cookie

I want to put cookie to my page.
Here's the logic :
When I load the page, I want it to load a popup or bootstrap modal. But the modal only load once when the browser is active. And only will load again when the browser tab is closed or exits the browser application. I have used session to do this, but I prefer to use cookie for personal preferences.
Is there a way to do this with javascript?
I've tried with $(window).load(), and $(window).on('beforeunload',function());
Javascript :
<script type="text/javascript">
$(window).load(function () {
if( $.cookie('firstLoad') == 'unloaded' || $.cookie('firstLoad') == 'null' || $.cookie('firstLoad') == null ) {
$('#openLoading').modal('show');
var time_exp = 1;
$.cookie('firstLoad','loaded',{ expires: time_exp });
}
});
$(window).on('beforeunload', function (){
alert($.cookie('firstLoad'));
$.cookie('firstLoad','unloaded');
});
</script>
The problem is sometimes the app browser will execute location.reload() and will reset the cookie in some way and make the popup appear again.
Please provide solution, thanks.
PS : the var time_exp and expires : time_exp is a last resort if the unload doesn't work
The beforeunload event doesn't just fire when the tab is closed. It will fire whenever the user goes to a new page in the same tab, including a page on your site. So you are resetting your cookie every time the user navigates between pages.
There is no event you can use to tell you the user is leaving your site specifically. The closest you can do is not set an expires, so that the cookie will automatically be deleted when the browser exits.
You could put a close button in the modal, and set a cookie when it is clicked so you know the user has viewed the modal and you don't need to show it again for however long you decide.

Firefox addons-sdk stop tab loading function

Please write a function that stop load tab. To refresh a simple reload (); complaint is on the page sdk stopping function.
Use this code to stop the most recently used tab from loading its page. (note: the page has to be loading something)
const { getMostRecentBrowserWindow } = require('sdk/window/utils');
getMostRecentBrowserWindow().gBrowser.selectedBrowser.stop();
This one here reloads: getMostRecentBrowserWindow().gBrowser.selectedBrowser.reload();

Disable main browser, display popup with window.open and do some actions

I'm using ajax for some function for my website, in success function I write some code:
success: function(html) {
window.open("http://localhost/demo/index.php",'name','height=243,width=800');
}
There are some problems I met:
This page (popup window) can not load css
I can disable main browser
I want to redirect main window after popup window is closed
How can I do that?
P/S: If I can't do that, how do I display popup when success function is called?
1. This page (popup window) can not load css
There might be some problem with your style urls. Check them in browser console.
2. I can disable main browser
You can't disable main browser, but As per my understanding you are trying to block page elements. So block UI can help. Use block UI to block your page contents and it will be automatically removed when you will redirect your main page.
3. I want to redirect main window after popup window is closed
You need to bind a custom redirect method to your window close to redirect your main page when window is closed.
success: function (html) {
myWindow = window.open("http://localhost/demo/index.php", 'name', 'height=243,width=800');
myWindow.onbeforeunload = function () {
return RedirectPage();
}
function RedirectPage() {
window.location.location.href = "http://www.yoursite.com";
}
You can redirect main window using:
window.parent.location.href = "http://www.site.com";
Regarding popup window not loading css, it might be issue with improper CSS include URL's in popup page html.

Executing function when pop-up window with redirects inside is closed

I'm showing the pop-up window from the page. The popup has one page, which then redirects to another (LinkedIn authorization one, to be clear), and then, after the successful login, the initial authorization page is opened again.
I want to reload the parent page when the popup is closed, but cannot do this.
The code is the following:
function OpenAuthorizePopUp() {
var w = window.open("AuthorizePage.aspx", "PopUp", "width=450,height=540");
w.onunload = function () {
SubmitPage();
};
return false;
}
function SubmitPage() {
alert("SUBMIT!");
}
The issue here is that SubmitPage() function is called not when window is closed, but just after the popup is shown. I guess it's because of redirect inside the popup, and unload is raised when we move from the first page.
Is there a way to catch the actual moment when the window is closed in this case?
So the flow of the popup goes like this:
1. Your page -> 2. LinkedIn Page -> 3. Your Page
Can you change the URL that LinkedIn sends you back to (3.) ? If so, have LinkedIn send you back to a page that has window.onunload = window.parent.SubmitPage; on it.
If you can't- ie, LinkedIn always sends you back to the first page- make the third page test for a successful connection to LinkedIn and if it's present, execute window.onunload = window.parent.SubmitPage;.
A possible improvement to your idea would be to just have the "success" page call window.parent.SubmitPage(); and then window.close(); since you won't need the popup anymore.
Found the solution. Instead of accessing parent window with window.parent, wrote
opener.location.reload(true);
And it works.

Categories