1

I have a javascript src that i need to add to some of the pages in a site.

for example <script type="text/javascript" src="http:abcxyz.com/zzz"></script>

I want to add this conditionally on a .ascx page - if the Request.ServerVariables["SCRIPT_NAME"] ends with certain criteria.

The ascx language is vb, and there is no code behind.

Thanks

2 Answers 2

1

If you make it so the tag is runat=server, you should be able to conditionally add the code as script:

<head runat="server">

  <% If Request.ServerVariables("SCRIPT_NAME") = "value" Then %>
    <script type="text/javascript" src="whatever.js"></script>
  <% Else %>
    <script type="text/javascript" src="whatever_else.js"></script>
  <% End If %>

</head>
Sign up to request clarification or add additional context in comments.

5 Comments

the problem is that it needs to be in the body, and if i make the body runat=server, it messes up other things..
@user228058 - why does it need to be in the body? A script reference should be able to be placed anywhere on the page. Conversely, since you would need to have a <form runat="server"> on the page somewhere, you could place this same code inside the form tags.
This should work without runat=server anywhere on the page, as should my example code.
You need to change == to = (it's not C#)
@James - tru dat. runat=server is my habit because I rarely use scripting. It can totally be removed. Oh, and changed. Switching back and forth too much.
0

No code behind, but still code in front right?

This will probably do it...

<%= If(Request.ServerVariables("SCRIPT_NAME").EndsWith("<criteria>"), "<script type='text/javascript' src='http:abcxyz.com/zzz'></script>", "")%>

1 Comment

thanks a lot. I tried this, and i'm getting an expression expected error. do you know why that might be?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.