How to get rid of this message form the alert? - javascript

I have an alert which appears to state that data has been submitted. Problem is thought that when the alert appears it comes up with this dialogue in the alert "Prevent this page from creating additional dialogues" or something like that.
How can I get rid of this from the alert? If I can't get rid of it then is there are way I can get something like a prompt box or a confirmation box which only has an 'OK' button to appear because I don't want like the "Prevent this page from creating additional dialogues" message.
Below is my code:
function submitform()
{
var fieldvalue = $("#QandA").val();
$.post("insertQuestion.php", $("#QandA").serialize() ,function(data){
alert("Your Details for this Session has been submitted");
var QandAO = document.getElementById("QandA");
QandAO.submit();
});
}

You can't stop this from happening. Good browsers provide the option to prevent scripts from spawning alerts because alerts are often annoying. If this option could be revoked by the script, what has been gained?

The reason why you get it for that alert and not for the other one is because this alert will be fired programmaticly at the difference to be initiated by the user action.
open firebug
alert('hello')
Everytime this alert is called it show a conventional alert without the checkbox
setInterval(function(){
alert('hello')
}, 500)
The first alert is normal the second one got the checkbox and allow you to prevent future alert because as steve says "good" browser can kind of understand something and have built in "supposedly helper"
Steve is right when he says that the message can not be removed but you can only control how the alert is open.
Perso I will not recommend you to use native alert because while the alert is open all the code on the page remain halted meaning nothing happen behind the popup. This can be annoying depending on your app.

Related

Remove a Leave button using javascript [duplicate]

I have the following beforeunload function which I have stolen from sonewhere else....
$().ready(function() {
$("#posManagerLoginForm").trigger("submit");
$(window).bind("beforeunload", function(){
window.setTimeout(function () {
window.location = "home.htm";
}, 0);
window.onbeforeunload = null; // necessary to prevent infinite loop that kills your browser
return "Press 'Stay On Page' to go to Reporting Manager home";
});
});
Regardless of what option I select I get navigated to home.htm. Is there a way that I can make the dialog box an ok button instead of the default "Leave Page" or "Stay on page" options?
Or perhaps someone else could make a suggestion on hot to better handle?
thanks
You cannot override the styling of the onbeforeunload dialog. Believe me, I tried this before in my earlier projects.
Reference: http://msdn.microsoft.com/en-us/library/ms536907%28VS.85%29.aspx
It is built into the browser object, and you have no control over it.
You can however set your own dialog to show when the onbeforeunload event triggers, but it will not disable that the regular one will show. Quite annoying, yes.
The reason you're still getting redirected is because you're actually doing nothing to prevent it.
If you want to open an alert box before the form gets submitted, make sure the default behaviour is prevented (which is to submit the form), then redirect after OK has been clicked like this:
$().ready(function() {
$("#posManagerLoginForm").submit(function( event ) {
event.preventDefault();
alert("Press 'OK' to go to Reporting Manager home");
window.location = "home.htm";
});
});
Though not sure what the use of this would be. If you wanted to stay on the form if a different button is pressed (say 'Cancel' for example), then you'd rather want to use a 'confirm' like this:
$().ready(function() {
$("#posManagerLoginForm").submit(function( event ) {
event.preventDefault();
if confirm(("Press 'OK' to go to Reporting Manager home"))
window.location = "home.htm";
});
});
You could replace the alert or confirm with a custom dialog box too, depending on what library you're using. Just make sure you put window.location = "home.htm" inside the dialog's function, otherwise it will execute immediately.
As an example, you may want to have a look into jQuery UI's dialog here: https://jqueryui.com/dialog/

Detecting when user hits back button

I'm trying to detect when the user hits the "back" button in their browser.
When clicking on the link and then getting back, nothing happens:
<script src="http://code.jquery.com/jquery-latest.min.js"
type="text/javascript"></script>
<script>
$( window ).on( "navigate", function( event, data ) {
console.log( data.state );
});
</script>
Link
I was expecting a message in the log.
When the user checks some checkboxes, then go to another page and then returns back, I want to reset the form, so that all checkboxes are empty.
This isn't working because your javascript is not running on google.com. If you are on the page with your javascript and click the back button, you may see the console.log message for a split second, but it is a race between the browser loading previous page and the console loading your message that will determine if you see the message or not.
I had the same problem. The best solution I found was to use a checksum in the initial post. You can then compare the checksum and see if it had been used before, if it was then you can reset everything.

How to disable browser feature

I am developing a project where user gets a conformation page. I want user not to click back or close tab or reload.
Now either I need to disable the browser features or get back button,tab close event, or reload event to java script so that I could take the needed steps to prevent my data to get lost.
I have used this:
window.onbeforeunload = function()
{
return "Try This";
};
But this get called even when I click a button that redirects the page.
If you just want to have the alert, understanding that the user is ultimately in control and can bypass your alert, then do what you're doing but use a flag that disables it when you're navigating and don't want the alert. E.g.:
var warnWhenLeaving = true;
window.onbeforeunload = function() {
if (warnWhenLeaving) {
return "your message here";
}
};
then in a click handler on the link/button/whatever that moves the user on that you don't want this to pop up on:
warnWhenLeaving = false;
In a comment you asked:
can i know that what user has clicked when alert is generated with this function. That is can i know what user has clicked (leave this page/stay on page)
The answer is: Sort of, but not really; you're almost certainly better off not trying to.
But: If you see your onbeforeunload function run, then you know the user is leaving the page and the browser is likely to show them your message. The browsers I'm familiar with handle the popup like an alert: All JavaScript code on the page is blocked while the popup is there. So if you schedule a callback via setTimeout, you won't get the callback if they leave and you will if they stay:
window.onbeforeunload = function() {
if (warnWhenLeaving) {
setTimeout(function() {
display("You stayed, yay!");
}, 0);
return "No, don't go!";
}
};
Live Example
So in theory, if you get the callback, they stayed; if you see an unload event, they left. (Note that there are very few things you can do in an unload event.)
I've tried that on current Chrome, current Firefox, IE8, and IE11: It works on all of those. Whether it will work in the next release of any of them is anybody's guess. Whether it works reliably on mobile browsers is something you'd have to test, and again could change.

Cucumber, confirm browser dialog which shows when page starts to refresh

When one scenario is done, the page refreshes (in the website there is a javascript modal implemented before refreshing the page which asks the user "Are you sure you want to leave the page?"). I need to confirm that modal. But when I create the step for that, I always get this error:
Then User clicks "Leave this page" button in the popup at online reg form
no alert open
(Session info: chrome=35.0.1916.114)
and the code
And(/^User clicks "([^"]*)" button in the popup at online reg form$/) do |button|
wait = Selenium::WebDriver::Wait.new
alert = wait.until { page.driver.browser.switch_to.alert }
alert.accept
end
Does anyone know how to handle this?
You need to override the confirm dialogue method in javascript to always return true
page.evaluate_script('window.confirm = function() { return true; }')
use this line before the line of code that triggers the dialogue to popup and it will always accept it no more steps needed :)

Ruby Watir: Clicking OK on JavaScript Alerts?

Seems none of the code I've tried has any affect. My intention is to close any and all JavaScript prompts that may come up by hitting the "OK" button. Problem is, my script has no affect on the prompts that come up. In other words, it does nothing.
Here's what I have:
fx = FireWatir::Firefox.start(somepage)
fx.startClicker("OK")
fx.button(:id, "OK").click
fx.button(:id, "CONFIRM").click
The HTML:
<script type="text/javascript">
alert("Alert!");
window.confirm("Confirm?");
</script>
The text in the prompts can change, my intention is to hit OK regardless of what is inside the alert/confirm prompt.
PS: I'm running Ubuntu.
The best way is to stop pop-ups from triggering at all.
require 'watir'
b = Watir::Browser.start "http://somepagewithdialogs"
# don't return anything for alert
b.execute_script("window.alert = function() {}")
# return some string for prompt to simulate user entering it
b.execute_script("window.prompt = function() {return 'my name'}")
# return null for prompt to simulate clicking Cancel
b.execute_script("window.prompt = function() {return null}")
# return true for confirm to simulate clicking OK
b.execute_script("window.confirm = function() {return true}")
# return false for confirm to simulate clicking Cancel
b.execute_script("window.confirm = function() {return false}")
# interact with some element which would trigger the pop up
b.button(:id => "dialogTrigger").click
See: http://watirmelon.com/2010/10/31/dismissing-pesky-javascript-dialogs-with-watir/ for more detail.
this was asked an eternity ago so I'm just adding something a little more updated that did it for me
#browser.alert.exists?
#browser.alert.ok
#browser.alert.close
first one will return a boolean
second one will ok whatever action you are prompted to do
and third one will close the alert with no
Pop ups are black magic to me. Did you try the solutions from here?
http://wiki.openqa.org/display/WTR/Pop+Ups
http://wiki.openqa.org/display/WTR/JavaScript+Pop+Ups
I would also suggest posting your question at watir-general.
I think your fx.button(:id, "OK").click was waiting state changed.
But javascript dialog does not change state.
So your watir will be waiting forever.
If not like that,I do not know.
The action will not change state, never return it.
So it needs click no wait.
When I use watir(not firewatir), #ie.button(:id, 'OK').click_no_wait.
Then better wait 1~3 second for popup.
Then as you like.
And moreover if you want control msg-box(popup), need to AutoIT.
--This is sample for wait msg-box and click ok for IE popup--
autoit=WIN32OLE.new('AutoItX3.Control')
autoit.WinWait('Windows Internet Explorer')
autoit.WinActive('Windows Internet Explorer')
autoit.ControlClick('Windows Internet Explorer','','OK')
It is possible that completely I don't understand what you mean.
If so ignore this.
Check out /var/lib/gems/1.8/gems/firewatir-1.6.5/unittests/html/JavascriptClick.html (assuming that's where your firewatir gem is installed). I ran the test and it worked for me. Maybe reading the test will give you some insight into how startClicker is supposed to work.

Categories