0

I am trying to get a server side code working with JavaScript. What am I doing wrong?

function openUp(name)
{
    document.getElementById(name).style.display =  <%if (Request.ServerVariables["HTTP_USER_AGENT"].Contains("FireFox"))Then %>
        response.write('table-row'); <%else%> response.write('block');
}
1
  • For starters, you're trying to build JavaScript dynamically, which is almost always a bad idea. Use classes and [data-*] attributes to pass data to the client side, and keep your scripts in external .js files. Commented Sep 26, 2012 at 14:05

2 Answers 2

2

Your Response.Write statement (which is not properly cased) should be inside the <% %> tags. You also seem to have an odd mixture of C# and VB.NET in your code. Which of the following are you using?

C#

document.getElementById(name).style.display =  
    <% if (Request.ServerVariables["HTTP_USER_AGENT"].Contains("FireFox"))
           Response.Write("'table-row'");
       else
           Response.Write("'block'"); %>; // Semi-colon outside of %> is for JavaScript

VB.NET

document.getElementById(name).style.display =  
    <% If (Request.ServerVariables("HTTP_USER_AGENT").Contains("FireFox")) Then
           Response.Write("'table-row'") 
       Else
           Response.Write("'block'")
       End If %>; // Semi-colon outside of %> is for JavaScript
Sign up to request clarification or add additional context in comments.

Comments

0

To answer your question, you can update your code to;

C#

document.getElementById(name).style.display = '<%Response.Write((Request.ServerVariables["HTTP_USER_AGENT"].Contains("FireFox") ? "table-row": "block")); %>';

VB

document.getElementById(name).style.display = '<%Response.Write(IIF(Request.ServerVariables["HTTP_USER_AGENT"].Contains("FireFox"), "table-row", "block")) %>';

Comments

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.