Inactive way to add javascript to page footer in asp.net

Whenever I want to add a javascript library programmatically, say for example jquery, it is usually assumed that there is a placeholder in the footer of my page, and then calling the codebehind method, which will reference the src as parameter and return the htmlgeneric control, which then is added to this placeholder .

Is this another easiest way to do this, even with .net 4.0 out?

+4
source share
4 answers

I think the best way is to use the RegisterStartupScript method:
http://msdn.microsoft.com/en-us/library/z9h4dk8y.aspx

And even better in your case is RegisterClientScriptInclude:
http://msdn.microsoft.com/en-us/library/kx145dw2.aspx

EDIT:
Here's a sample RegisterClientScriptInclude:

 if (!Page.ClientScript.IsClientScriptIncludeRegistered("myJsInclude")) Page.ClientScript.RegisterClientScriptInclude("myJsInclude", "myJsFile.js"); 

EDIT2:
Here is an example with include with RegisterStartupScript:

 string jsBlock = "<script src='myJsFile.js'></script>"; if (!Page.ClientScript.IsStartupScriptRegistered("myJsInclude")) Page.ClientScript.RegisterStartupScript(typeof(string), "myJsInclude", jsBlock, false); 

You should add things like language = "text / javascript" to the script tag, but I didn’t add them for readability.

+7
source

Sorry ... I decided to postpone my comment to the answer.

I personally add all my JS to the ScriptManager. This helps reduce the number of Http calls the page should make.

 ScriptManager1.CompositeScript.Scripts.Add(New ScriptReference("~/Page/To/Jquery.js")) 

But this is only if you are already using ScriptManager on your page

In addition, if you do not want to add it from CodeBehind, you can do it directly on your page.

 <ScriptManager> <CompositeScript> <Scripts> <-- your scripts in here --> </Scripts> </CompositeScript> </ScriptManager> 

So, having done this, you will be able to add all your JS in one HTTP request, and not have several different requests at once.

Then, in the ScriptManager tag, you can add LoadScriptsBeforeUI="false" so that they are placed at the bottom of the page.

+3
source

Sorry, but this was never the cleanest way to embed a script in an asp.net page.

Look at the ClientScript object. There are several methods that will suit your needs without resorting to placeholders.

+1
source

ScriptManager is a good way to do this, as mentioned above. If you do not use MS Ajax and ScriptManager, I suggest you write your own control. This should be a very simple operation. Add the public variable List and override the RenderContents method to view the list of rows and display on the page. Code example:

 public class CustomScriptManager : WebControl { private List<string> scripts = new List<string>(); public List<string> Scripts { get { return scripts; } set { scripts = value; } } protected override void RenderContents(HtmlTextWriter writer) { foreach (string script in scripts) { writer.Write("<script language=\"JavaScript\" type=\"text/javascript\" src=\"" + script + "\"></script>"); } } } 

PS I did not check the code above, but I understand what you understand.

+1
source

Source: https://habr.com/ru/post/1301249/


All Articles