Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Include Javascript file on condition in Page_Load

Tags:

c#

webforms

Example:

if (!IsPostBack) {
   if ( condition ) {
      //  load javascript file like
      //  <script type="text/javascript" src = "https://File_AA.js"></script>
  }  
  else
  {
      //  <script type="text/javascript" src = "https://File_BB.js"></script>
 }
}

Based on a condition, how can I load a specific JavaScript file? Is there a better way to include only ONE file based on a condition?

like image 609
Steve42 Avatar asked Dec 19 '25 04:12

Steve42


1 Answers

 string file_a =  "file_a.js" ;
 string file_b =  "file_b.js" ;
 string selectedFile;
 if(condition)
    selectedFile = file_a;
 else
    selectedFile = file_b;
 string scriptText = string.Format(
             System.Globalization.CultureInfo.InvariantCulture,
             "<script type='text/javascript' src='{0}'></script>\n",
             selectedFile
             );
this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(),"ScriptFile", scriptText);
like image 100
RAK Avatar answered Dec 21 '25 20:12

RAK



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!