0

I have one .php file, which contain html, php and javascript.

The javascript block is used for determine browser, if browser is not IE, javascript will execute html block that contains php

This is javascript code

<script type="text/javascript">
     var isIE = false || !!document.documentMode;
     if( !isIE ){
           //it will execute html block below
     }
</script>

This is the html that contains php code

<div class=center>
    <div class="flip">
        <div class="flip-child">

            <div class="front">
                <img src="<?php ABSPATH; ?>/new/logo/logo.png" alt="front" />
            </div>

            <div class="back">
                <a href="<?php ABSPATH; ?>/new/menu.html"> <img src="<?php ABSPATH; ?>/new/logo/back.png" alt="back" /> </a>
            </div>

        </div>
    </div>
</div>

That all is in one .php file. If isIE variable is false, i want to execute that html block, how to do?

1
  • The obvious answer is you can't execute PHP once the page has been rendered and JavaScript kicks in. (see programmers.stackexchange.com/questions/171203/…) . This particular problem can easily be solved by checking the client user agent in PHP though. Commented Mar 12, 2016 at 8:40

2 Answers 2

4
<div class="center" id="htmlblock" style="display: none;">
    <div class="flip">
        <div class="flip-child">

            <div class="front">
                <img src="<?php echo ABSPATH; ?>/new/logo/logo.png" alt="front" />
            </div>

            <div class="back">
                <a href="<?php echo ABSPATH; ?>/new/menu.html"> <img src="<?php echo ABSPATH; ?>/new/logo/back.png" alt="back" /> </a>
            </div>

        </div>
    </div>
</div>

<script type="text/javascript">
     var isIE = false || !!document.documentMode;
     if( !isIE ){
           //it will execute html block below
           document.getElementById("htmlblock").style.display = "block";
     }
</script>

This code will make it such that if the browser is not IE, the div will be displayed, else it will remain hidden (with that display: none; style).

Note that I modified the div to give it an id of htmlblock, and changed all the <?php ABSPATH; ?> to <?php echo ABSPATH; ?>

Sign up to request clarification or add additional context in comments.

2 Comments

I know this is 2016 and it probably doesn't matter anymore but documentMode is IE 8 + just worth noting in case one expects any dinosaurs visiting their site.
thank you, but with echo before ABSPATH doesn't work, so i still use without echo
0

As suggested in http://ageekandhisblog.com/use-php-to-detect-internet-explorer-11-and-below/ you can do the following server-side.

<?php
if (preg_match('~MSIE|Internet Explorer~i', $_SERVER['HTTP_USER_AGENT']) || (strpos($_SERVER['HTTP_USER_AGENT'], 'Trident/7.0; rv:11.0') !== false)): ?>
<div class=center>
    <div class="flip">
        <div class="flip-child">

            <div class="front">
                <img src="<?php ABSPATH; ?>/new/logo/logo.png" alt="front" />
            </div>

            <div class="back">
                <a href="<?php ABSPATH; ?>/new/menu.html"> <img src="<?php ABSPATH; ?>/new/logo/back.png" alt="back" /> </a>
            </div>

        </div>
    </div>
</div>
<?php endif; ?>

Note This relies on the user agent which can be spoofed.

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.