Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8185685
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T01:56:07+00:00 2026-06-07T01:56:07+00:00

How can I dynamically change the <script> src attribute from the Page_Load ? When

  • 0

How can I dynamically change the <script> src attribute from the Page_Load?

When I say dynamic, I really mean that it’s the same page Default.aspx, but each refresh a different js source is referenced. The logic behind which js file to select is of no concern, merely the mechanism to set it.

I tried:

<script id="script1" runat="server" language="javascript" src="a.js" type="text/javascript"></script>

But script1 is not available on the .cs side. I know I can change it on the .aspx side by using the <% %> tags, but I don’t want to have my logic embedded like that in my .aspx. There must be a way to do this on the .cs side in Page_Load?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T01:56:08+00:00Added an answer on June 7, 2026 at 1:56 am

    On your page load handler you could have something like this

    string csurl = null;
    string csname = "myscript";
    if (condition)
    {
        csurl = "~/a.js";
    }
    else    
    {  
        csurl = "~/b.js";
    }
    
    if (!Page.ClientScript.IsClientScriptIncludeRegistered(cstype, csname))
    {
        Page.ClientScript.RegisterClientScriptInclude(
            this.GetType(), 
            csname, 
            ResolveClientUrl(csurl));
    }
    

    or

     string csurl = null;
     if (condition)
     {
         csurl = "a.js";
     }
     else    
     {  
         csurl = "b.js";
     }
     Literal script = new Literal();
     script.Text = string.Format(
         @"<script src=""{0}"" type=""text/javascript""></script>",csurl);
     Page.Header.Controls.Add(script);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want a script where I can change the font dynamically when the page
I have a Query that the datasources it is set to can change dynamically.
How can I write a script that will change an input field's text value,
I have a page where the user can dynamically add file upload boxes. Adding
I am writing a container framework that can dynamically deploy a Jar file containing
Does anyone know how I can Dynamically create an installation package that installs files
Has anyone come across a software that can dynamically stream an arbitrary source identified
While importing a python script from another script I want the script code that
I can dynamically append select control to DOM,after appending it to DOM,I wanna change
I'd like to change icon of the node according to the attribute automatically/dynamically in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.