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&navpanes=0&scrollbar=0' }" type="application/pdf" style="width: 100%; height: 800px !important;">
<object data-bind="attr: { data: data.files()[currentPage()] + '#toolbar=0&navpanes=0&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
–
–
–
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&navpanes=0&scrollbar=0' }\" type=\"application/pdf\" style=\"width: 100%; height: 800px !important;\">");
$('#targetElement').append("<object data-bind=\"attr: { data: data.files()[currentPage()] + '#toolbar=0&navpanes=0&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.