user17753
user17753

Reputation: 3161

dynamically changing the script tag source on C# side

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?

Upvotes: 3

Views: 8046

Answers (4)

aquinas
aquinas

Reputation: 23796

Just use the script manager:

E.g.,

string jsName = condition ? "a.js" : "b.js";

ScriptManager.GetCurrent(this).Scripts.Add(new ScriptReference(jsName));

Upvotes: 0

Claudio Redi
Claudio Redi

Reputation: 68400

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);

Upvotes: 6

freefaller
freefaller

Reputation: 19953

Unfortunately I don't think you have much choice other than to use <%= %>.

Unless somebody knows different (if so, please comment, and I will change) using the runat="server" attribute on a <script> block will mean the block will be executed on the server side.

UPDATE

Sorry, slightly misunderstood your question.

I thought you were saying you didn't want to use <% %> to set the src attribute of the <script> block. I now understand you meant creating individual <script> blocks for each file, being display via logical <%If x Then%><%Else%><%End If%> blocks.

As other's have already stated (and I have hinted at), you can set the src attribute using <%=GetSrcPath()%> and have the logic in the GetSrcPath method in your code-behind.

Upvotes: 0

wsanville
wsanville

Reputation: 37516

There's a few ways to do this. One approach is to make a protected or public method in your codebehind that returns a string, which returns the correct URL to your Javascript based on your criteria. Then, just call that method from your markup, like this:

<script language="javascript" src="<%=GetJavaScriptUrl() %>" type="text/javascript"></script>

This example assumes you call the method in your codebehind GetJavaScriptUrl.

Upvotes: 4

Related Questions