i was establishing communication between website and flutter i want to fire message on button click and i have implemented the code below.
<button onclick="buttonClick()" type="button">Cancel</button>
<script>
function buttonClick() {
window.addEventListener("flutterInAppWebViewPlatformReady", function(event) {
window.flutter_inappwebview.callHandler('cancelbuttonState', 1, true,['msg','CancelbuttonClicked'], {test:'Go-Back'}).then(function(result) {
alert("Hello! Cancel Buttton clicked!!");
console.log('Hello! Cancel Buttton clicked!!');
});
});
}
</script>
in flutter side i have implemented the following to get the message
controller.addJavaScriptHandler(
handlerName: "cancelbuttonState",
callback: (args) async {
print('Cancel Button clicked');
return args.reduce((curr, next) => curr + next);
});
but both JavaScript and app dint give me console print thanks.
You are executing the below line too late inside the body of the cancel function. You will need to add the listener much earlier for the callback to be registered.
In other words, you are listening for an event that is already finished.
window.addEventListener("flutterInAppWebViewPlatformReady" .....
Reorganize your code in a fashion similiar to this:
<button onclick="buttonClick()" type="button">Cancel</button>
<script>
isAppReady = false;
window.addEventListener("flutterInAppWebViewPlatformReady", function (event) {
isAppReady = true;
});
function buttonClick() {
if (isAppReady) {
window.flutter_inappwebview.callHandler('cancelbuttonState', 1, true, ['msg', 'CancelbuttonClicked'], { test: 'Go-Back' }).then(function (result) {
alert("Hello! Cancel Buttton clicked!!");
console.log('Hello! Cancel Buttton clicked!!');
});
}
}
</script>
I need your help. I'm working with JavaScript and I'm not able to configure how to work on window.onbeforeunload.
here's my simple code:
window.onbeforeunload = function () {
if (!confirm("some message here")) {
return "Are you sure?";
} else{
return false;
}
}
I just need to work the code in closing the browser, reloading page and or exiting tab menu. I need that it will not also work on any links when I press links in my web body or page. Do you have any idea or solution for this?
I hope you can help me with this. I appreciate your kindness.
Thank you
You could try that:
$(document).on('mousedown', 'a[href]', offBeforeUnload)
.on('mouseleave', 'a[href]', function () {
$(window).on('beforeunload', windowBeforeUnload);
});
function offBeforeUnload(event) {
$(window).off('beforeunload');
}
function windowBeforeUnload() {
if (!confirm("some message here")) {
return "Are you sure?";
} else{
return false;
}
}
$(window).on('beforeunload', windowBeforeUnload);
DEMO
How can I display "Are you sure you want to leave the page?" when the user actually tries to close the page (click the X button on the browser window or tab) not when he tries to navigate away from the page (click on another link).
My client wants a message to appear when the user tries to close the page "Are you sure you want to leave the page? You still have items in your shopping cart."
Unfortunately $(window).bind('beforeunload') doesn't fire only when the user closes the page.
jQuery:
function checkCart() {
$.ajax({
url : 'index.php?route=module/cart/check',
type : 'POST',
dataType : 'json',
success : function (result) {
if (result) {
$(window).bind('beforeunload', function(){
return 'leave?';
});
}
}
})
}
You can do this by using JQuery.
For example ,
click here
Your JQuery will be,
$(document).ready(function(){
$('a').on('mousedown', stopNavigate);
$('a').on('mouseleave', function () {
$(window).on('beforeunload', function(){
return 'Are you sure you want to leave?';
});
});
});
function stopNavigate(){
$(window).off('beforeunload');
}
And to get the Leave message alert will be,
$(window).on('beforeunload', function(){
return 'Are you sure you want to leave?';
});
$(window).on('unload', function(){
logout();
});
This solution works in all browsers and I have tested it.
Try javascript into your Ajax
window.onbeforeunload = function(){
return 'Are you sure you want to leave?';
};
Reference link
Example 2:
document.getElementsByClassName('eStore_buy_now_button')[0].onclick = function(){
window.btn_clicked = true;
};
window.onbeforeunload = function(){
if(!window.btn_clicked){
return 'You must click "Buy Now" to make payment and finish your order. If you leave now your order will be canceled.';
}
};
Here it will alert the user every time he leaves the page, until he clicks on the button.
DEMO: http://jsfiddle.net/DerekL/GSWbB/show/
Credit should go here:
how to detect if a link was clicked when window.onbeforeunload is triggered?
Basically, the solution adds a listener to detect if a link or window caused the unload event to fire.
var link_was_clicked = false;
document.addEventListener("click", function(e) {
if (e.target.nodeName.toLowerCase() === 'a') {
link_was_clicked = true;
}
}, true);
window.onbeforeunload = function(e) {
if(link_was_clicked) {
return;
}
return confirm('Are you sure?');
}
As indicated here https://stackoverflow.com/a/1632004/330867, you can implement it by "filtering" what is originating the exit of this page.
As mentionned in the comments, here's a new version of the code in the other question, which also include the ajax request you make in your question :
var canExit = true;
// For every function that will call an ajax query, you need to set the var "canExit" to false, then set it to false once the ajax is finished.
function checkCart() {
canExit = false;
$.ajax({
url : 'index.php?route=module/cart/check',
type : 'POST',
dataType : 'json',
success : function (result) {
if (result) {
canExit = true;
}
}
})
}
$(document).on('click', 'a', function() {canExit = true;}); // can exit if it's a link
$(window).on('beforeunload', function() {
if (canExit) return null; // null will allow exit without a question
// Else, just return the message you want to display
return "Do you really want to close?";
});
Important: You shouldn't have a global variable defined (here canExit), this is here for simpler version.
Note that you can't override completely the confirm message (at least in chrome). The message you return will only be prepended to the one given by Chrome. Here's the reason : How can I override the OnBeforeUnload dialog and replace it with my own?
Try this, loading data via ajax and displaying through return statement.
<script type="text/javascript">
function closeWindow(){
var Data = $.ajax({
type : "POST",
url : "file.txt", //loading a simple text file for sample.
cache : false,
global : false,
async : false,
success : function(data) {
return data;
}
}).responseText;
return "Are you sure you want to leave the page? You still have "+Data+" items in your shopping cart";
}
window.onbeforeunload = closeWindow;
</script>
You can try 'onbeforeunload' event.
Also take a look at this-
Dialog box runs for 1 sec and disappears?
while testing this code I found it to be working absolutely fine with the firefox browser checked it with firebug but it doesn't work with chrome browser
here is my code :
<script type="text/javascript">
window.onfocus = startCheck;
var dualLogin;
function startCheck() {
dualLogin = setInterval(function () {
checkTable();
}, 20000);
}
function checkTable() {
$.ajax({
url:"<%= url_for :controller => 'xyz',:action => 'abc'%>",
cache:false,
dataType:'json',
success:function (data) {
if (data.status=='dual')
{
logout();
}
}
})
}
window.onblur = stopCheck;
function stopCheck() {
window.clearInterval(dualLogin);
}
function logout(){
alert('you are to be logged out');
window.location.replace("/registeration_page");
}
</script>
can any one help thanks in advance.
I create a jsfiidle.
have a look. click on result box for focus and click on other for blur.
I want to open pop-up on exit (Not "stay on page or leave" box). I can't get this working. Where do I make mistake ?
function poponload()
{
testwindow = window.open("http://www.google.com", "mywindow",
"location=1,status=1,scrollbars=1,width=5,height=5,left=100,top=100");
}
$(document).ready(function () {
window.onbeforeunload = function () {
poponload();
});
$(window).unload(function () {
poponload();
});
});
Your code has a syntax error. You have an extraneous parenthesis after the onbeforeunload function assignment. Remove it and see if that fixes your issue.
window.onbeforeunload = function () {
poponload();
};