发布新的iframe src,监控并重新加载更改


Post new iframe src, monitor and reload on change

我在这里读过多个关于通过表单post等改变iframe src URL的问题,但是似乎没有我想要完成的。如果可能的话,使用jQuery或JavaScript。

所以我有index.html包含1 iframe。

<html>
..
<iframe id="stream" marginwidth="0" marginheight="0" scrolling=no allowtransparency=true frameborder="0" framespacing="0" webkitallowfullscreen allowfullscreen name="stream" width="100%" height="100%" src="https://youtu.be/JIdaZ2EJ1Zk">
..
etc
</html>

在一个单独的页面上,我有一个form.html文件,这将让我为iframe发布一个新的src url。我试图改变iframe的src在index.htmlform.html没有运气。

例如,用户将看到初始iframe src。

每5秒运行一次JavaScript检查"检测iframe src更改"或更好的方法是受欢迎的。然后,它将把iframe重新加载到表单发布的url中。很抱歉,我的问题太啰嗦了,我不擅长解释……

Nutshell: form post新的src url,检测src url的变化,只重载新的iframe。

您也可以在php中使用单个页面

<?php
    if (isset($_POST['url'])) {
?>
    <iframe id="stream" marginwidth="0" marginheight="0"
        scrolling=no allowtransparency=true frameborder="0"
        framespacing="0" webkitallowfullscreen allowfullscreen
        name="stream" width="100%" height="100%"
        src="<?php echo $_POST['url']; ?>">
<?php
    }else{
?>
    <form action="" method="post">
        <input type='text' value="" name="url" />
        <input name="submit" type="submit" value="Show" />
    </form>
<?php
    }
?>

请注意,这是一个非常快速和简单的解决方案,使用一个页面。

你也可以使用2个不同的页面,索引和表单,也许从地址

检索iframe的url
http://blablbal?url=foo
并使用$_GET 访问url属性

注意这种方式不需要刷新,但是您可以保留用于设置url的表单始终可见,并且仍然使用单页解决方案:

<html>  
    <form action="" method="post">
        <input type='text' name="url" 
            value="<?php echo(isset($_POST['url'])? $_POST['url']:'');?>" />
        <input name="submit" type="submit" value="Show" />
    </form>
    <hr>
    <?php
        if (isset($_POST['url'])) {
    ?>
        <iframe id="stream" marginwidth="0" marginheight="0"
            scrolling=no allowtransparency=true frameborder="0"
            framespacing="0" webkitallowfullscreen allowfullscreen
            name="stream" width="100%" height="100%"
            src="<?php echo $_POST['url']; ?>">
    <?php
        }
    ?>
</html>