views:

44

answers:

2

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

A: 

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>", "")%>
James Gaunt
thanks a lot. I tried this, and i'm getting an expression expected error. do you know why that might be?
Have you done it exactly as shown, works fine for me.... strange...
James Gaunt
+1  A: 

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>
Joel Etherton
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.
Joel Etherton
This should work without runat=server anywhere on the page, as should my example code.
James Gaunt
You need to change == to = (it's not C#)
James Gaunt
@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.
Joel Etherton