How to block alert box through jQuery - javascript

I want to block alert box if it is present in code. Im using an api that tells me the search result of my website and if any user enter
<script>alert('Just teasing')</script>
then it shows an alert box on my page how can i stop this alert?

First of all you should sanitize you input as #Nikita commented.
If you want to accept JavaScript and only disable alert you can replace the window.alert function.
window.alert = function() { /* do nothing here */ }
Now calling alert won't do anything.

When presenting the search results back to the user you need to ensure you HTML encode the output so the user would see the script rather than it being executed.

Related

Customize Javascript Alert NOT onclick

I have absolutely no idea about Javascript and don't know if it is even possible. Anyway, I found a script that customizes Javascript Alert Box. It works nicely. The issue I am having is that this customization works based on 'onClick' as below:
// Standard Dialogs
$("#alert").on( 'click', function () {
reset();
alertify.alert("This is an alert dialog");
return false;
});
However, my Alert Box is generated by different code, and it either generates Alert Box or redirects to another page, but has nothing to do with onClick function. My question is:
is it possible to customize the Alert Box which is generated outside of the onClick function? If so, could anyone let me know how?
And my sincere apologies if that subject was already explained somewhere - I was looking for an answer but could not find.
Thank you
If you're trying to have the alert pop up initially without prompt it appears you will need:
alertify.alert("This is an alert dialog");
to be pasted outside of the function.
This will trigger the alert when the page loads as long as your tags are at the bottom of your html &/or you've used a document.ready statement.
My other interpretation of your above message is that the code above works and another alert box doesn't in which case can you please show us the code from the code that is not displaying as customised.

multipule actions if statement in javascript

I want to create a multipule action if statement, for example this is my script now:
if (document.getElementById("username").value==null || document.getElementById("username").value=="")
{
alert("username must be filled out");
return false;
}
and I want that in the same time as the alert pops up to add another action like change to path to some image or something llike that..
anyone know how to do this?
thanks,
Lian
The alert popup in browsers is a kind of modal form.
From the moment you call the alert, user interaction with the browser is limited to the popup buttons and you cannot do anything else.
You can't offer the user "multiple actions" with an alert popup.
Try creating your own modal box as your message box.
because once you pop up an alert in js the code below will not executed unless you hit the ok button

Assign a value on OK button of JavaScript Alert Messagebox

I know that confirm message-box of JavaScript can be assigned a value. Now, i also want an alert message-box (OK button) to have a value, So that i can use it in my server-side code in ASP.Net.
Is this possible?
It seems you haven't understood well about the alertbox.
In any javascript function if alert message is shown then after you click the ok button only next statement will get executed.
But if u really want to assign a value to ok button then just declare a variable and assign a value to that particular variable in your code after the alert code. Sample code will look like below.
function fnMyFunction(){
var myval="";
//your code goes here
alert('Your Message');
myval="your value";
}

window.onbeforeunload returns user to same page

On leaving a page with a form, I want to save the changes made without any user confirmation.
Having found this question I adapted one of the answers as follows. Note that I am not using return:
function setConfirmUnload(on) {
window.onbeforeunload = (on) ? unloadMessage : null;
}
function unloadMessage() {
alert('Gonna save now');
setTimeout(validate_submit,500);
}
This is only triggered when the user fills some value in the form. If an element value changes, I use
setConfirmUnload(true);
Result in FF23: when user clicks somewhere to leave the page, the alert is shown, validate_submit() is executed on "OK", new page appears - BUT now the alert reappears and the user is returned to the original page on "OK". Why? Can somebody confirm this behaviour?
The window.onbeforeunload method causes a confirm box to appear containing whatever message is returned by the function assigned to it. You can put alerts and other stuff in this method, but it will always end with a confirm message. If your function returns nothing, it will still give a message (determined by browser) saying "are you sure you want to leave this page" or something along those lines.

Refresh the page after click OK on alert box

In checkout page of my magento store, I need to clear some inputs when an alert box is showed and user press OK. Is possible to do that?
I have no control over the javascript alert. So I think in a script that detect an alert with a specific message and clear inputs when button of that alert is clicked.
UPDATE
Fixed!
file: opcheckout.js
line: 888
I add location.reload(); because document.location.reload(true); not work on IE. Thanks everybody!
Probably try overriding default alert and write a custom function like below,
var c_alert = alert;
window.alert = function (str) { //override default alert
c_alert(str + ' my message');
location.reload();
//or write code to clear input fields here
}
//below seems to be triggered from somewhere where you don't have control.
alert('test');
Javascript
if(confirm('your confirmation text')){
document.location.reload(true);
}
Make it refresh after the alert. The javascript code shouldn't execute before the alert is gone

Categories

Resources