Programmatically add javascrip refernce (or full script) in page header


/ Published in: C#
Save to your folder(s)

Place javascrip refernce or complete javascript code in page header from ascx or aspx file.


Copy this code and paste it in your HTML
  1. // add reference like this
  2.  
  3. HtmlGenericControl script1 = new HtmlGenericControl("script");
  4. script1.Attributes.Add("type", "text/javascript");
  5. script1.Attributes.Add("src", "../js/prototype.js");
  6. this.Page.Header.Controls.Add(script1);
  7.  
  8.  
  9. // add full script like this
  10. // first create string which contains your javascript
  11. string YahooScript = "";
  12. YahooScript = "\n"
  13. + "YAHOO.util.Event.onContentReady(\"izbornik\", function () {\n"
  14. + "var oMenuBar = new YAHOO.widget.MenuBar(\"izbornik\", {autosubmenudisplay: true, hidedelay: 750, lazyload: true });\n"
  15. + "oMenuBar.render();\n"
  16. + "});\n";
  17.  
  18. // and then place javascript in header
  19. HtmlGenericControl script3 = new HtmlGenericControl("script");
  20. script3.Attributes.Add("type", "text/javascript");
  21. script3.InnerHtml = YahooScript;
  22. this.Page.Header.Controls.Add(script3);
  23.  
  24.  
  25. // in aspx page you can add script using Literal control
  26. // place Literal control in page Header in code view.
  27. // create your script string
  28. string temaSkripta = "";
  29. temaSkripta = "\n"
  30. + "function kojaTema()\n"
  31. + "{"
  32. + "var tema = '" + tema + "';\n"
  33. + "return tema;"
  34. + "}";
  35.  
  36. // find Literal in heder and make your string it's text.
  37. Literal L = (Literal)Header.FindControl("Literal1");
  38. L.Text = temaSkripta;

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.