Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8" />
- <title>window this</title>
- <script type="text/javascript" src="http://code.jquery.com/jquery-1.6.4.min.js"></script>
- <script type="text/javascript">
- (function($)
- {
- function init()
- {
- window.name = 'parent';
- window.meh = function()
- {
- alert('meh:' + this.name);
- }
- $("#create").click(function()
- {
- // Open an empty popup we're going to fill in js (so that demo requires only one file)
- var W = window.open('', 'myWindow');
- W.name = 'child';
- // Using writeln, which is extacly like referencing a js file for my example
- W.document.writeln('<html><head>'+
- '<title>newWindow</title>'+
- '</head><body>'+
- '<script type="text/javascript">'+
- //'alert(this.name);'+ // this is resolved to W (popup window), so alert outputs 'child'
- //'window.opener.meh();'+ // guess what, 'child' or 'parent'? 'parent' of course !
- 'var MEH = window.opener.meh; MEH();'+ // and now ? yeah 'child'
- '</scr'+'ipt>'+
- '</body></html>');
- });
- var MEH = window.meh; MEH(); // 'parent'
- /*
- Take a careful look at the 2 MEH() calls.
- `this` resolution depends on where the js source that triggers the call stack is coming from.
- Not we're the function currently executed is written (meh function declaration).
- */
- }
- $(init);
- })(jQuery);
- </script>
- </head>
- <body>
- <input type="button" id="create" value="Click Me" />
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement