Remeber iFram SRC when refreshing parent page

旧巷老猫 提交于 2019-12-11 05:09:02

问题


I have an intranet I am building, and am learning web languages as I go.

I have a basic html/php page that contains an iFrame, which most of my content is loaded into.

I also have a login box that is a popup modal within the main page. When you fill in this popup and hit login, it refreshes the main page, so that it knows you are logged in, and can display your name etc etc.

However, this means that the iframe SRC also resets to its default.

Is there a way to retain whatever the current SRC of the iframe is, when refreshing the parent page?


回答1:


You can set a session variable to keep the current src.

In your case I would do something like the following:

All pages which are loaded in the iframe can contain the code to set the session var.

<?php

session_start(); // before any output to the user-agent / browser /screen!!!

$_SESSION['last_visited'] = $_SERVER['REQUEST_URI'];

And on the page which displays the iframe you can simply do something like:

<?php

session_start(); // before any output to the user-agent / browser /screen!!!

echo '<iframe src="', $_SESSION['last_visited'], '"></iframe>';

Regarding your question about saving the URL in the database.

You could do that but I would just save it in a cookie.




回答2:


HTML

<form action="whatever" method="post">
...
    <input type="hidden" id="iframeSrc" name="iframeSrc"/>
    <input type="submit" onclick="addSrc()" value="Log in"/>
</form>

<iframe id="mybox" src="somepage"></iframe>

JS

function addSrc() {
   document.getElementById('iframeSrc').value = document.getElementById('mybox').src;
}

PHP

$iframeSrc = $_POST['iframeSrc'];
...
echo "<iframe id=\"mybox\" src=\"$iframeSrc\"></iframe>";



回答3:


Why not just use Cookies

function createCookie(name,value,days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        var expires = "; expires="+date.toGMTString();
    }
    else var expires = "";
    document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}

function eraseCookie(name) {
    createCookie(name,"",-1);
}


来源:https://stackoverflow.com/questions/7390220/remeber-ifram-src-when-refreshing-parent-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!