在Javascript中回显PHP代码

时间:2017-11-12 04:18:00

标签: javascript php

我无法解决此问题。如果页面在iframe中,我试图回复一些PHP代码到HTML。下面是我尝试回应的PHP代码的一个简短示例(我尝试回复的真实代码要长得多)...

<?php $pic='https://cdn.pixabay.com/photo/2015/06/19/17/58/sample-815141_960_720.jpg'; ?>

<div id="frameid"></div>
<script type="text/javascript">     
    function inIframe () {
        try {
            return window.self !== window.top;
        } catch (e) {
            return true;
        }
    }
    if (inIframe()) {
        var node = document.getElementById('frameid');
        node.innerHTML('<?php echo '<img width="100%" src="'.$pic.'"></img>'; ?>');
    }
</script>

3 个答案:

答案 0 :(得分:-1)

似乎有一些报价问题。您可以尝试以下代码:

<div id="frameid"></div>
<script type="text/javascript">     
    function inIframe () {
        try {
            return window.self !== window.top;
        } catch (e) {
            return true;
        }
    }
    if (inIframe()) {
        var node = document.getElementById('frameid');
        node.innerHTML(<?php echo "...something involving html and php variables...."; ?>);
    }
</script>

答案 1 :(得分:-1)

你能试试吗? var pic =; 的innerHTML(&#34;&#34); 我认为将js与php分开更清楚,并且重新编写

答案 2 :(得分:-2)

您在使用单引号时遇到麻烦,您可以考虑将此功能(取自WP https://developer.wordpress.org/reference/functions/_wp_specialchars/)转义为引用(单引号和双引号)

// Plugin function function coolName(triggers) { $(document).ready(function () { var offsetY = window.pageYOffset; // Close with 'esc' key $(document).keyup(function (e) { if (e.keyCode == 27) triggers.close.trigger('click'); }); triggers.body.click(function () { offsetY = window.pageYOffset; // Block scrolling triggers.body.css({ 'position': 'fixed', 'color': '#FFFF00', 'backgroundColor': '#00D', 'top': -offsetY + 'px' }) }); // Show popup triggers.holder.show(); triggers.close.click(function () { // Allow scrolling again triggers.body.css({ 'position': 'static', 'color': '', 'backgroundColor': '' }) }); /** * Remove the following scrollTop()'s if you want. * just a UI tweak that the user would expect. **/ // Make the page stay at the position it was at before the overlay triggers.win.scrollTop(offsetY); // Reset the overlay scroll position to the top triggers.stuff.scrollTop(0); // Hide popup triggers.holder.hide() }); } // Implementation coolName({ close: $('.mobile-filters-close'), open: $('.filters-toggle'), holder: $('.mobile-filters-wrapper'), stuff: $('.mobile-filters-wrapper .filters') })

您可以在其中定义

{{1}}