繁体   English   中英

将变量从JavaScript传递到PHP并重新加载DIV / iframe

[英]Passing variable from JavaScript to PHP and reload DIV/iframe

我有一个网站,其中包含DIV中的iframe。 iframe的宽度应取决于浏览器的宽度。

因此,我使用JavaScript函数和EventListener来获取浏览器的宽度。 很好

现在,我必须将确定的宽度从JavaScript传递到我的iframe,然后重新加载。 这是我的HTML / PHP代码,其中包含index.html的iframe:

<div id="scout">
    <?php
        $iframeWidth = "<script>document.write(iframeWidthFromJS)</script>";
        echo "iframe width is " . $iframeWidth;
    ?>

    <iframe src="http://cpcms.scout" width="<?php echo $iframeWidth; ?>" height="600"></iframe>
</div>

这是我的JavaScript,它不起作用:

window.addEventListener('resize', getWindowSize);
window.onload(getWindowSize());

function getWindowSize()
{
    var iframeWidthFromJS = 800;

    if (typeof (window.innerWidth) == 'number')
    {
        // Get current browser width
        width = window.innerWidth;
        iframeWidthFromJS = width;

        alert(iframeWidthFromJS);

        // Reload the DIV to apply new iframe width
        $( "#scout" ).load(window.location.href + " #scout" );
    }
}

确定宽度有效,但不能将其传递给iframe。

不能只使用width="100%"

修改您的代码,使包含您iframe的父div也获得新的宽度

<?php
            $iframeWidth = "<script>document.write(iframeWidthFromJS)</script>";
            echo "iframe width is " . $iframeWidth;
        ?>

    <div id="scout" width="<?php echo $iframeWidth; ?>">
        <iframe src="http://cpcms.scout" width="<?php echo $iframeWidth; ?>" height="600"></iframe>
    </div>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM