Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 12 of 12
  1. #1
    New to the CF scene
    Join Date
    Jun 2008
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Javascript hack to block alerts

    Hi Guys,

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

    Thanks in advance.

  • #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,852
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    Could you block entire javascript support from browser ?
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #3
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    Quote Originally Posted by shezam View Post
    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.
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • Users who have thanked Kor for this post:

    Philip M (06-22-2008)

  • #4
    Regular Coder
    Join Date
    Jun 2007
    Location
    USA
    Posts
    527
    Thanks
    26
    Thanked 74 Times in 72 Posts
    Code:
    window.alert = function() {};
    Trinithis

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,920
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Quote Originally Posted by Trinithis View Post
    Code:
    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.

  • #6
    Regular Coder
    Join Date
    Jun 2007
    Location
    USA
    Posts
    527
    Thanks
    26
    Thanked 74 Times in 72 Posts
    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.
    Trinithis

  • #7
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,920
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Quote Originally Posted by Trinithis View Post

    First class functions enable a ton neat an interesting things.
    I have no idea what you mean. Do you?

  • #8
    Regular Coder
    Join Date
    Jun 2007
    Location
    USA
    Posts
    527
    Thanks
    26
    Thanked 74 Times in 72 Posts
    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:
    Code:
     . . .
    if(debug)
      alert(x);
     . . .
    if(debug)
      alert(y);
     . . .
    vs a simpler
    Code:
    if(!debug)
      window.alert = function() {};
     . . .
    alert(x);
     . . .
    alert(y);
     . . .
    And all this is due to first-class functions, hence my statement in my previous post.
    Last edited by Trinithis; 06-22-2008 at 12:19 AM.
    Trinithis

  • #9
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,920
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    =Trinithis;703284]
    Granted, this is a little contrived......
    No, that is not true. It is completely and utterly contrived. 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. )

    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
    Last edited by Philip M; 06-22-2008 at 08:11 AM.

  • #10
    Regular Coder
    Join Date
    Jun 2007
    Location
    USA
    Posts
    527
    Thanks
    26
    Thanked 74 Times in 72 Posts
    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.
    Trinithis

  • #11
    jkd
    jkd is offline
    Senior Coder jkd's Avatar
    Join Date
    May 2002
    Location
    metro DC
    Posts
    3,163
    Thanks
    1
    Thanked 18 Times in 18 Posts
    Umm, any legitimate reason? Why not for a Greasemonkey script to make your own surfing experience more pleasant? Something like:

    Code:
    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.

  • #12
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,920
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Quote Originally Posted by jkd View Post
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •