1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Disable a Javascript Alert from a Child Iframe

Discussion in 'HTML & JavaScript' started by lanbo, Apr 10, 2012.

  1. lanbo

    lanbo Jr. VIP Jr. VIP Premium Member

    Joined:
    Aug 23, 2009
    Messages:
    3,436
    Likes Received:
    595
    Home Page:
    If my website (site1) is iframing another site (site2), is there anyway to disable any javascript alerts sent from site2?
     
  2. jazzc

    jazzc Moderator Staff Member Moderator Jr. VIP

    Joined:
    Jan 27, 2009
    Messages:
    2,468
    Likes Received:
    10,148
    No, there isn't, unless you own both.
     
  3. madsem

    madsem Junior Member

    Joined:
    Aug 23, 2010
    Messages:
    121
    Likes Received:
    40
    There COULD be one method...

    Only thing you could do is to load the site with php in file_get_contents() .
    So you have the website data as a string, remove all javascript, and then put that in an iframe.

    So basically every time a visitor hits your site, your script scrapes the other page, and then removes all javscript with a regex, then wraps that into an iframe.
    This method should work and give you your desired results.
     
  4. madsem

    madsem Junior Member

    Joined:
    Aug 23, 2010
    Messages:
    121
    Likes Received:
    40
    So like this somehow:

    Create a file named whatever you want...

    PHP:
    $html file_get_contents('http://www.example.com/');
    $html preg_replace('#<script.*</script>#is'''$html);

    // render the bastard
    echo $html;
    And then take this file and putit in an iframe where you want... Instead of the child iframe you use now.
    (might need some adjustments, not tested the script. and it can certainly be done better too)
     
  5. roster67

    roster67 Registered Member

    Joined:
    Mar 27, 2012
    Messages:
    69
    Likes Received:
    10
    The Php workaround is the best solution.

    DocShell could be an other but not cross browser.
     
  6. Chris22

    Chris22 Regular Member

    Joined:
    Sep 29, 2010
    Messages:
    400
    Likes Received:
    1,059
  7. lanbo

    lanbo Jr. VIP Jr. VIP Premium Member

    Joined:
    Aug 23, 2009
    Messages:
    3,436
    Likes Received:
    595
    Home Page:
    I needed to disable js on a domain that's not mine + must be loaded on the client's side (they have to be logged in)
     
  8. madsem

    madsem Junior Member

    Joined:
    Aug 23, 2010
    Messages:
    121
    Likes Received:
    40
    yeah, and I gave you the solution :) It would work
     
  9. jazzc

    jazzc Moderator Staff Member Moderator Jr. VIP

    Joined:
    Jan 27, 2009
    Messages:
    2,468
    Likes Received:
    10,148
    Your solution is wrong -read what you quoted carefully. He needs the user to be loading the original third site, not a "capture" served from his server as you suggest. As I mentioned before, it is not possible to run your js in another site - browsers do not allow it.
     
  10. madsem

    madsem Junior Member

    Joined:
    Aug 23, 2010
    Messages:
    121
    Likes Received:
    40
    I read it again, I don't see why it shouldn't work, he doesn't say that he wants to run his own javascript on a third party site, he says he wants to disable javascript alerts coming from the thrid party site, and that is possible the way I showed?

    quote:
    is there anyway to disable any javascript alerts sent from site2?

    You said it's not possible to disable unless you own both, and that is not correct.

    Maybe the OP should give a few more details, in his second post he says the user should be logged in, logged in where? On site 1 or site 2? If the user should be logged in site 2 it wouldn't work, obviously, but that was not part of the original question, hence you also couldn't know what he meant originally? :)

    As long as he wants to only display a page it will work, as soon as his implementation requires site2 to do any serverside jobs it won't... happy?
     
    Last edited: Apr 22, 2012
  11. Chris22

    Chris22 Regular Member

    Joined:
    Sep 29, 2010
    Messages:
    400
    Likes Received:
    1,059
    I read somewhere that you could reassign window.alert.
     
  12. jazzc

    jazzc Moderator Staff Member Moderator Jr. VIP

    Joined:
    Jan 27, 2009
    Messages:
    2,468
    Likes Received:
    10,148
    OP said:
    When you run a php script on your server, the server loads that page - NOT the client. As far as the client browser is concerned, the origin of the content is your server NOT the other site. Which is clearly not what the op wants. Those js alerts will not be "sent from site2" anymore, the third party code with be executing in the context of the OP's site (which is useless to the OP).

    On site2. Given that this is a BH forum, isn't that more than obvious? Thus, we could easily know what he mean originally :D
     
  13. madsem

    madsem Junior Member

    Joined:
    Aug 23, 2010
    Messages:
    121
    Likes Received:
    40
    Well ok, in this context yeah. I didn't understand him correctly. I was thinking he simply wants to display a page and get rid of the alerts it's sending or something along those lines :D So yeah, if you need site2 to execute any server or client side jobs that are hosted on site2's server it won't work that is correct :(