views:

665

answers:

5

I'm looking to somehow get jQuery inserted into every page with the MINIMUM of code written... in other words I don't want to write <script language="javascript" src="PATH TO JQUERY"></script> on every single aspx page.

So far I've thought of using a Base class and inserting a "Response.Write" into the "page load" event. Besides that, what are my other options... web.config? something else?

Bonus points for a Webforms/MVC solution. Thanks for any suggestions.

EDIT: Found an answer that I like at http://www.codeproject.com/KB/aspnet/scriptregister.aspx The author, like myself, disliked the standard implementation of ClientScript.RegisterClientScriptBlock because of various reasons (like MSFT insistance of having a FORM runat=server tag) and built a better mousetrap.

A: 

If you are already including

<html><head><title>Title</title></head><body>...</body></html>

in every single page, I'm sure you can squeeze in a

<script ...></script>

But I would still agree with Ken: take a look at master pages.

Justice
A: 

not knowing you're architecture, this may be hard to answer generically.

However, may i suggest that if you do end up having to modify all your pages, set them up for using an include, should u have to update your jquery version or add a $(document).ready() function or add any jquery plugin's, you can modify the include and not have to re-modify all the pages.

Roy Rico
I thought "#includes" were frowned upon as a Classic ASP only solution, not suitable for ASP.NET. Right?
Robert
A: 

The best practice is to load javascript at the bottom of your pages to prevent your script slowing your initial page load.

Since you've also specifically mentioned jquery I would recommend using this code towards the end of your masterpage:

<script src="http://www.google.com/jsapi"&gt;&lt;/script&gt;
<script>
  // Load jQuery
    google.load("jquery", "1");
</script>

The advantage of this is that your users will probably already have this copy of jquery cached in their browser, and you'll always be using the latest version to boot :)

If you're using a masterpage, you won't need to have the script "inserted into every page".

Bayard Randel
+1  A: 

You can hook to any event of the page life cycle in the global asax:

void Application_PreRequestHandlerExecute(object sender, EventArgs e)
{
    Page page = this.Context.CurrentHandler as Page;
    if( page != null )
        page.Load += new EventHandler(page_Load);
}

void page_Load(object sender, EventArgs e)
{
    Page page = (Page)this.Context.CurrentHandler;
    page.ClientScript.RegisterClientScriptInclude("jquery", jqueryurl);
}

Ps. I think you can do this with an http module as well.

eglasius
+2  A: 

Use MasterPages and put it at the top. Some people say put it at the bottom but after the first time it's loaded it cached anyways and if you are going to manipulate the DOM I personally would rather do it before my user really starts to see the page being rendered. Having it at the bottom could definitely cause this to happen and could be confusing to the user and result in a bad user experience.

Slee
The recommendation to load javascript last comes from Yahoo's web performance team and is also recommended in the book High Performance Web Sites published by O'reilly. The only caveat is using scripts calling document.write
Bayard Randel