Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams if(DetectIE()) <embed data-bind="attr: { src: data.files()[currentPage()] + '#toolbar=0&amp;navpanes=0&amp;scrollbar=0' }" type="application/pdf" style="width: 100%; height: 800px !important;"> <object data-bind="attr: { data: data.files()[currentPage()] + '#toolbar=0&amp;navpanes=0&amp;scrollbar=0' }" type="application/pdf" width="100%" height="600px"></object>

I have a javascript code to detect if the current browser is Internet explorer or not . If it is a IE then <embed> tag is used otherwise <object> tag is used.

Any suggestions or help would be appreciated.

Thanks in advance

Just use @if (DetectIE()) { // embed tag } else { // object tag } statement. But what is DetectIE() do? Tetsuya Yamamoto Nov 1, 2018 at 4:18 Nope, you cannot use JS method with Razor if statement. Place the if statement inside the <script> tag and append the element with something like $.append('<embed>...</embed>') . Tetsuya Yamamoto Nov 1, 2018 at 4:22 It might be easier to use Request.Browser to determine the browser in the controller, and then just pass a bool property to your view indicating if its IE or not (as a view model property or a ViewBag property) user3559349 Nov 1, 2018 at 4:24

Since DetectIE() is a JS function, you can't use it as comparison to Razor @if block. You should put it inside <script> with jQuery.append() to append the proper tag into target element:

<script>
$(function() {
    // other stuff
    if (DetectIE())
        $('#targetElement').append("<embed data-bind=\"attr: { src: data.files()[currentPage()] + '#toolbar=0&amp;navpanes=0&amp;scrollbar=0' }\" type=\"application/pdf\" style=\"width: 100%; height: 800px !important;\">");
        $('#targetElement').append("<object data-bind=\"attr: { data: data.files()[currentPage()] + '#toolbar=0&amp;navpanes=0&amp;scrollbar=0' }\" type=\"application/pdf\" width=\"100%\" height=\"600px\"></object>");
</script>

Example of the target element:

<div id="targetElement" class="primary-content"></div>

If you want to check any version of IE from controller side, you can use this:

bool isIE = (Request.Browser.Browser == "IE" || Request.Browser.Browser == "InternetExplorer");

And then pass the value to ViewBag:

ViewBag.IsIE = isIE;

JS usage example

if (@ViewBag.IsIE)
   // render embed tag
   // render object tag

You cannot use javascript function inside razor directly..so bettter use Request .Browser to Get the Browsername

@{bool IsIE = false ;
if (Request.Browser.Type.ToUpper().Contains("IE"))
    IsIE = true;
@if (IsIE)
    // Your Razor here
    //  Your Razor here
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.