刷新父页面时记住iFram SRC

发布于 2024-12-03 22:28:05 字数 241 浏览 0 评论 0原文

我正在构建一个内部网,并且正在学习网络语言。

我有一个基本的 html/php 页面,其中包含一个 iFrame,我的大部分内容都加载到其中。

我还有一个登录框,它是主页中的弹出模式。 当您填写此弹出窗口并点击登录时,它会刷新主页,以便它知道您已登录,并可以显示您的姓名等。

但是,这意味着 iframe SRC 也会重置为其默认值。

有没有办法在刷新父页面时保留 iframe 的当前 SRC 是什么?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

佞臣 2024-12-10 22:28:05

您可以设置会话变量来保留当前的src。

在您的情况下,我会执行如下操作:

iframe 中加载的所有页面都可以包含设置会话变量的代码。

<?php

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

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

在显示 iframe 的页面上,您可以简单地执行以下操作:

<?php

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

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

关于将 URL 保存在数据库中的问题。

你可以这样做,但我会把它保存在 cookie 中。

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.

守望孤独 2024-12-10 22:28:05

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>";

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>";
爱你是孤单的心事 2024-12-10 22:28:05

为什么不直接使用 Cookie

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);
}

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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文