Re:globalFromsandbox.js

From disqus.com, 2 Months ago, written in JavaScript, viewed 3 times. This paste is a reply to Re: moment.js from disqus.com - view diff
URL https://pastebin.freepbx.org/view/478b79f6 Embed
Download Paste or View Raw
  1. define('core/host/globalFromSandbox',['require'],function (require) {
  2.     'use strict';
  3.  
  4.     var document = window.document;
  5.     var frame = document.createElement('iframe');
  6.  
  7.     frame.style.display = 'none';
  8.  
  9.     /**
  10.      * Utility to get unaltered version of global built-ins
  11.      * like JSON, Math, etc.
  12.      *
  13.      * This is needed because host pages can modify the properties
  14.      * of built-in objects.
  15.      *
  16.      * It works by opening a srcless iframe and returning
  17.      * iframe's (unaltered) instance of the built-in object.
  18.      *
  19.      * Falls back to fallbackWin's instance of the built-in
  20.      * if it cannot get a non-null instance from the iframe.
  21.      *
  22.      * usage:
  23.      *     var JSON = globalFromSandbox('JSON', window);
  24.      *
  25.      * @param   {string} name The name of the built-in
  26.      * @param   {Window} fallbackWin A fallback `window` object to use if sandbox fails
  27.      * @returns {Object}      Instance of the built-in
  28.      */
  29.     return function globalFromSandbox(name, fallbackWin) {
  30.         var fallback = fallbackWin && fallbackWin[name] || null;
  31.  
  32.         try {
  33.             // Make sure we're attached to the "current" body since
  34.             // sometimes the iframe gets removed by publishers changing
  35.             // innerHTML etc. We also need to make sure that `frame.parentNode`
  36.             // is equal to `document.body` since things like Turbolinks completely
  37.             // rewrite the whole document innerHTML causing document.body to be
  38.             // replaced and `frame.parentNode` pointing to the "old" instance of
  39.             // the body element. In this case `frame.contentWindow` still is `null`
  40.             // since the frame is not placed on the actual DOM.
  41.             if (frame.parentNode !== document.body)
  42.                 document.body.appendChild(frame);
  43.  
  44.             return frame.contentWindow[name] || fallback;
  45.         } catch (err) {
  46.             return fallback;
  47.         }
  48.     };
  49. });
  50.  
  51. // https://c.disquscdn.com/next/next-core/core/host/globalFromSandbox.js

Replies to Re:globalFromsandbox.js rss

Title Name Language When
Re: iframe.js disqus.com javascript 2 Months ago.

Reply to "Re:globalFromsandbox.js"

Here you can reply to the paste above