Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript: Why sometimes alert() does not work but console.log() does?

Tags:

From time to time, I face a very intriguing bug. My javascript code does not display an alert(msg) during execution, but if I use a console.log(msg) it does show up in the console. What could prevent alert() from displaying?

Thanks a lot

like image 402
siebmanb Avatar asked Jan 22 '14 08:01

siebmanb


People also ask

Why is alert on JavaScript not working?

The reason alert() does not work is because previously you have checked "prevent this page from creating additional dialoug" checkbox. lets take a look at this code. There will be two alert boxes if you run the code.

What is the difference between console log and alert in JavaScript?

alert() stops all interaction with the browser until the message is dismissed while console. log() just prints the message to the console.

Is JavaScript alert blocking?

One of the nice things about the built-in JavaScript alert is that - unlike virtually anything else in JavaScript - it's synchronous. It's completely blocking, and no other code will execute until it's been dismissed.

What happens when JavaScript runs the alert () function?

One useful function that's native to JavaScript is the alert() function. This function will display text in a dialog box that pops up on the screen. Before this function can work, we must first call the showAlert() function. JavaScript functions are called in response to events.


2 Answers

This is a very common problem, and everyone has faced this problem atleast once. The reason alert() does not work is because previously you have checked "prevent this page from creating additional dialoug" checkbox.

lets take a look at this code.

<script type="text/javascript">  var js_name = ['elem1', 'elem2']   for (var i = 0; i < js_name.length; i++) {     alert(js_name[i]);  };  </script> 

There will be two alert boxes if you run the code. If you check the "prevent this page from creating additional dialoug" checkbox and then refresh the page again you won't get alert box ever again.

Solution is you need to close that webpage and reopen again in the browser(don't need to close the entire browser). I am assuming you are using chrome. Internet Explorer or FireFox doesn't have this checkbox feature.

like image 168
user1906399 Avatar answered Oct 13 '22 02:10

user1906399


If you override alert function so it won't work

alert = function()  {  ... };  alert('hello') // won't show any alert 
like image 20
Tareq Salah Avatar answered Oct 13 '22 03:10

Tareq Salah