1

Bit of an weird request, but I'd like to be able to have a parent window:

  • Create a new child window on the same domain (using window.open)
  • Prevent any javascript running in the child window

Currently, I can do this within in the child window by including the following at the very top of the page:

<script>
  Function.prototype.call = function() {};
  Function.prototype.apply = function() {};
</script>

This pretty much stops any javascript in the child page from running. But is there any way for me to do this from the parent window? Can I somehow inject this code into the child page before any scripts have a chance to run?

Or is there any other way to do this?

Any help appreciated

bluepnume
  • 16,460
  • 8
  • 38
  • 48

3 Answers3

2

You can use bit of server side to remove all <script> tags and inline event handlers like onclick.

EDIT

using client side you can try to grab whole page using ajax remove the scripts there and write the content to newly created window. I don't think you will be able to use

$.get('page.html', function(html) {
    var html = $(html).find('script').remove().html();
});

because scripts may be executed when added to DOM, so maybe you will need to use a parser or just regex.

jcubic
  • 61,973
  • 54
  • 229
  • 402
2

window.open() returns a reference to the newly created window. If the window is on the same origin, you're free to access it.

var popup = window.open('/some/page.html');
popup.contentWindow // = the popup's `window` object

However, the code you've come up with doesn't work. You've likely seen that it breaks jQuery, which makes liberal use of call and apply, but it does nothing to stop normal function invocation.

josh3736
  • 139,160
  • 33
  • 216
  • 263
  • Yeah, you're absolutely correct, I was seeing it break jQuery and assuming the same effect for other js. Thanks for pointing this out to me. – bluepnume Mar 17 '13 at 07:36
0

If the child window is in an iframe? There has been a question like that, and the answer was - you can not do this easily with php serverside.

How do I disable scripts in my iframe?

Community
  • 1
  • 1
Martin Turjak
  • 20,896
  • 5
  • 56
  • 76
  • Nope, not an iframe, a child window created with `window.open(...)`. I have full control over the window, I just need to know how to run the above javascript-blocking code *before* the child page has a chance to load any other js. – bluepnume Mar 12 '13 at 20:13