...

View Full Version : Javascript hack to block alerts



shezam
06-21-2008, 04:24 PM
Hi Guys,

Any one know how to block javascript alerts using javascript, sounds easy doesn't it.

Thanks in advance.

abduraooft
06-21-2008, 04:28 PM
Could you block entire javascript support from browser ?

Kor
06-21-2008, 06:03 PM
Hi Guys,

Any one know how to block javascript alerts using javascript, sounds easy doesn't it.

Thanks in advance.
No, it does not sound easy, it sounds impossible. You can not block a language using the same language, as you can not rise yourself from the ground by simply pull of your hair with your hands.

Trinithis
06-21-2008, 06:44 PM
window.alert = function() {};

Philip M
06-21-2008, 07:13 PM
window.alert = function() {};


Is is a pity that you have responded in this way. There cannot be any legitimate reason for the OP's request, which he himself describes as a "hack".

<script type = "text/javascript">

alert ("Alert 1");
window.alert = function() {};
alert ("Alert 2");

</script>

Both Alert 1 and Alert 2 are triggered as one might expect. But if alert ("Alert 1") is removed then Alert 2 does not show. No idea why.

Trinithis
06-21-2008, 08:35 PM
Just because one cannot foresee a (legitimate) use for something does not mean there isnt one. There are always legitimate reasons. I've used similar code in a Greasemonkey script for instance . . . a site was abusing the alert feature.

Also such an assignment could be useful in debugging or something. First class functions enable a ton neat an interesting things.

As for your example, it works as expected for me. My version of firefox is 2.0.0.14.

Philip M
06-21-2008, 09:14 PM
First class functions enable a ton neat an interesting things.



I have no idea what you mean. Do you?

Trinithis
06-21-2008, 11:50 PM
EDIT: Whoah, I completely misread your post. I thought you said, "You have no idea what I mean. Do you?"

In answer your your actual post, what I mean is that learning that assignments like window.alert = function() {} exist is really nifty. For instance, I could (but probably won't ever) write code in which I use alert and change its behavior conditionally. I have however altered alert's behavior in a Clocker script so that whenever an alert occurred, the clock paused for the alert's duration.

For example, I could litter my code with alerts for debugging purposes, but I only want their behaviors to change conditionally. Instead of cluttering my code with a bunch of if-statements (yeah, yeah, I could have an alert2 function . . .), I could move my logic elsewhere, say in the actual alert variable.

Granted, this is a little contrived, but here's the gist:


. . .
if(debug)
alert(x);
. . .
if(debug)
alert(y);
. . .

vs a simpler


if(!debug)
window.alert = function() {};
. . .
alert(x);
. . .
alert(y);
. . .

And all this is due to first-class (http://en.wikipedia.org/wiki/First-class_function) functions, hence my statement in my previous post.

Philip M
06-22-2008, 07:50 AM
=Trinithis;703284]
Granted, this is a little contrived......

No, that is not true. It is completely and utterly contrived. :cool: And it is not "simpler". You remind me of an electrical engineer I knew who invariably devised something involving four transistors, three diodes, five capacitors and seven resistors to do the work of an ordinary on-off toggle switch (it only worked when you pressed a button. :D:D )

To come back to the main point, the OP cannot have any legitimate reason for wanting to block Javascript alerts - I don't believe his interest is in contrived test procedures. As I am paranoid I suspect that he wants to somehow inject this into other scripts, thus perhaps suppressing warning messages. Every so often we get a new poster who wants to circumvent something, or over-ride normal behaviour, especially browser security and pop-up blockers. I don't think that they ought to be encouraged. :(

"The world is made up of five basic elements. Earth, air, fire, water and bull****". - Philip M

Trinithis
06-22-2008, 05:55 PM
Okay fine. I won't answer these types of questions for new posters, but I will for those who I see regularly.

And you are right. Often I tinker around just for the heck of it.

jkd
06-22-2008, 06:15 PM
Umm, any legitimate reason? Why not for a Greasemonkey script to make your own surfing experience more pleasant? Something like:



window.alert = function(txt) {
var alert = document.body.appendChild(document.createElement("div"));
alert.appendChild(document.createTextNode(txt));
alert.className = "alert";
alert.style.left =(document.body.offsetWidth - alert.offsetWidth )/2;
alert.style.top = (document.body.offsetHeight - alert.offsetHeight)/2;
alert.onclick = function() { this.parentNode.removeChild(this) };
}


Modal-less alerts that don't block the UI. Overwriting alert() shouldn't affect browser-level alerts either, only ones spawned from Javascript. Please be kinder Philip -- your attitude was a bit much.

Philip M
06-22-2008, 08:16 PM
Umm, any legitimate reason? Why not for a Greasemonkey script to make your own surfing experience more pleasant?

Please be kinder Philip -- your attitude was a bit much.

I'll believe it when I see it.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum