Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Jon 10 posts 31 karma points
    Feb 09, 2010 @ 16:49
    Jon
    0

    Another approach for: Register javascript files, startup scripts

    Suggestions

    Umbraco version: 4.0.3 (and possible later?)

    Due to better SEO and faster rendering of sites with slow conections/heavy script dep. *
    - use another aproach for registring (1) javascript files and (2) startup scripts.

    * More info see: http://developer.yahoo.com/performance/rules.html#js_bottom

     

    1) As it is now core library method RegisterJavaScriptFile uses the header to put additional script include files; both backup and the .net standard way - wich is *flawed*  IMHO considering SEO and deffered loading)

    source:

                    System.Web.UI.HtmlControls.HtmlGenericControl include = new System.Web.UI.HtmlControls.HtmlGenericControl("link");
    include.ID = key;
    include.Attributes.Add("rel", "stylesheet");
    include.Attributes.Add("type", "text/css");
    include.Attributes.Add("href", url);

    if (p.Header != null)
    {
    if (p.Header.FindControl(key) == null)
    {
    p.Header.Controls.Add(include);
    }
    }
    else
    {
    //This is a fallback in case there is no header
    p.ClientScript.RegisterClientScriptBlock(p.GetType(), key, "<link rel='stylesheet' href='" + url + "' />");
    }

     

     

    Some suggested alternatives:

    * Use p.ClientScript.RegisterStartupScript instead, as this method place the script next to closing form

    * Use http-module to render all script below visible content -

    see http://weblogs.asp.net/omarzabir/archive/2008/04/06/fast-page-loading-by-moving-asp-net-ajax-scripts-after-visible-content.aspx for more info.

     

    2) There is no register startupscript block to use from library inside lets say a macro. (one has to use Umbraco library  - RegisterClientScriptBlock that renders the script with .NET RegisterClientScriptBlock

    Suggested solution:
    * expose a new method RegisterStartupScript utilizing .NET's method: Page.ClientScript.RegisterStartupScript .

  • Jon 10 posts 31 karma points
    Feb 09, 2010 @ 16:52
    Jon
    0

    sorry: misshap when trying to use the code formating(copy, and paste back and forth)

     

    This is the correct code for core library method RegisterJavaScriptFile

                    System.Web.UI.HtmlControls.HtmlGenericControl include = new System.Web.UI.HtmlControls.HtmlGenericControl("script");
    include.ID = key;
    include.Attributes.Add("type", "text/javascript");
    include.Attributes.Add("src", url);

    if (p.Header != null)
    {
    if (p.Header.FindControl(key) == null)
    {
    p.Header.Controls.Add(include);
    }
    }
    else
    {
    //This is a fallback in case there is no header
    p.ClientScript.RegisterClientScriptInclude(p.GetType(), key, url);

    }

    the other code was for registring a CSS-file.

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies