简体   繁体   中英

ASP.Net - How do I include an embedded JavaScript file from another project?

I've got two projects, one is a control library and another is my main project. From the control library I am currently using a user control and some css files which are embedded in the control library.

I can use the embedded CSS files in my main project by doing the following from my user control's PreRender event:

  // Register the default CSS resource
  string includeTemplate = "<link rel='stylesheet' text='text/css' href='{0}' />";
  string includeLocation = this.Page.ClientScript.GetWebResourceUrl(this.GetType(), "MyCompany.ControlLibrary.WebNotify.WebNotify.css");
  LiteralControl cssInclude = new LiteralControl(String.Format(includeTemplate, includeLocation));
  ((System.Web.UI.HtmlControls.HtmlHead)Page.Header).Controls.Add(cssInclude);

I thought it would then make sense to include all my javascript files in a similar fashion, so I included the embedded javascript file doing the following:

  // Register the js
  string includeTemplate = "<script type='text/javascript' src='{0}'></script>";
  string includeLocation = this.Page.ClientScript.GetWebResourceUrl(this.GetType(), "MyCompany.ControlLibrary.Scripts.MyScript.js");
  LiteralControl jsInclude = new LiteralControl(String.Format(includeTemplate, includeLocation));
  ((System.Web.UI.HtmlControls.HtmlHead)Page.Header).Controls.Add(jsInclude);

Now, the CSS all works perfectly, however my JS functions throw Object Required exceptions when trying to call them.

Am I going about this the correct way? Or is there a better way of including an embedded js file from another assembly into another project?

Seems fine; however, at this point I'd really be using client tools to determine whether or not everything's getting there and being used (fiddler/ie toolbar/firebug/etc).

If I had to guess, I would say your code is working, but whatever browser you're using is ignoring the javascript due to the script tag not having a closing tag (ie <script></script> opposed to <script />); for some reason some browsers are picky about that

Personnally, as others have suggested, use some tools such as FireBug for Firefox, Fiddler , or the Developer Tools for Internet Explorer to check what calls are being made to your servers, and what responses they are sending back - that's what BigBlondeViking's referring to.

I'd also check that you have marked the JS file as "build" in the solution - rather than the default of "take no action".

However, there is indeed a cleaner way of adding embedded script resouces, the ClientScriptManager's " RegisterClientScriptResource " method:

// Get a ClientScriptManager reference from the Page class.
ClientScriptManager cs = Page.ClientScript;

// Register the client resource with the page.
cs.RegisterClientScriptResource(rstype, 
     "MyCompany.ControlLibrary.Scripts.MyScript.js");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM