コード例 #1
0
        /// <summary>
        /// This allows us to ensure that the lazy load script is placed higher in the rendered html than either the css or the js rendered to the page.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        /// <remarks>
        /// TO accomplish this we detect if the http context has a special key (PlaceholderReplacementProcessing), if so we'll replace the first encountered
        /// token with the lazy load script and replace any other ones with empty text (so it is only loaded once). We need to do this because we have no
        /// idea if the developer has rendered the lazy css or lazy js first.
        /// </remarks>
        static void PlaceholdersReplaced(object sender, PlaceholdersReplacedEventArgs e)
        {
            if (e.HttpContext.Items[PlaceholderReplacementProcessing] is bool &&
                (bool)e.HttpContext.Items[PlaceholderReplacementProcessing])
            {
                var url           = GetWebResourceUrl(typeof(LazyLoadProvider), DependencyLoaderResourceName);
                var lazyScriptTag = string.Format(HtmlEmbedContants.ScriptEmbedWithSource, url, "");

                //replace the first occurance
                e.ReplacedText = e.ReplacedText.ReplaceFirst(LazyLoadScriptPlaceholder, lazyScriptTag);
                //replace the rest with nothing
                e.ReplacedText = e.ReplacedText.Replace(LazyLoadScriptPlaceholder, "");

                //set the flag to null so we don't process again
                e.HttpContext.Items[PlaceholderReplacementProcessing] = null;
            }
        }
コード例 #2
0
        /// <summary>
        /// This allows us to ensure that the lazy load script is placed higher in the rendered html than either the css or the js rendered to the page.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        /// <remarks>
        /// TO accomplish this we detect if the http context has a special key (PlaceholderReplacementProcessing), if so we'll replace the first encountered
        /// token with the lazy load script and replace any other ones with empty text (so it is only loaded once). We need to do this because we have no 
        /// idea if the developer has rendered the lazy css or lazy js first.
        /// </remarks>
        static void PlaceholdersReplaced(object sender, PlaceholdersReplacedEventArgs e)
        {

            if (e.HttpContext.Items[PlaceholderReplacementProcessing] is bool 
                && (bool)e.HttpContext.Items[PlaceholderReplacementProcessing])
            {
                var url = GetWebResourceUrl(typeof(LazyLoadProvider), DependencyLoaderResourceName);
                var lazyScriptTag = string.Format(HtmlEmbedContants.ScriptEmbedWithSource, url, "");

                //replace the first occurance
                e.ReplacedText = e.ReplacedText.ReplaceFirst(LazyLoadScriptPlaceholder, lazyScriptTag);
                //replace the rest with nothing
                e.ReplacedText = e.ReplacedText.Replace(LazyLoadScriptPlaceholder, "");

                //set the flag to null so we don't process again
                e.HttpContext.Items[PlaceholderReplacementProcessing] = null;
            }
        }