gpt4 book ai didi

iframe 在等待新页面加载时显示空白页面,而没有 iframe 则不会

转载 作者:IT王子 更新时间:2023-10-29 02:11:16 26 4
gpt4 key购买 nike

在这被遗忘之前,我只想说我觉得我已经尝试了除了更改应用程序以使用 AJAX 和使用 JSON 返回响应之外的所有方法,而不是像我现在所做的那样呈现新的 HTML 页面(使用 Go 的 https://github.com/unrolled/render package) - 重做所有事情需要大量工作,所以我希望有一个不涉及 AJAX + JSON 响应的解决方案

现在,每当我执行表单 POST 时,最后我都会渲染一个新的 HTML 页面:

render.HTML(w, http.StatusOK, "path/to/htmlfile/index", map[string]interface{}{
csrf.TemplateTag: csrf.TemplateField(r),
"passing some data": dataFromGo})

这在本地和生产环境中都能完美运行,但是当我将它放入 iframe 时,每当我单击表单提交时,在呈现新 HTML 页面之前,页面将变为空白(白色)1-2 秒(而在没有 iframe 的情况下访问时,原始 HTML 内容将在等待呈现新内容时保持显示)。我觉得这种行为只发生在 iframe 内部真的很奇怪。

有什么解决办法吗?我试过设置 <iframe name="my-iframe" ..>在我的表单中设置 target="my-iframe" , 我试过添加 sandbox到 iframe。我尝试过其他事后看来与解决我的问题完全无关的事情:)

顺便说一句,iframe src是另一个域,但这是不可避免的

最佳答案

根据我的实验,浏览器似乎会在响应正文的第一个字节到达时立即将 iframe 变为白色。我的猜测是您自己的代码或渲染包在实际处理完成之前发送字节。您可以使用此示例程序查看行为:

package main

import (
"fmt"
"log"
"net/http"
"time"
)

func main() {
http.DefaultServeMux.Handle("/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("content-type", "text/html")
fmt.Fprintln(w, `<iframe src="/frame"></iframe>`)
}))

http.DefaultServeMux.Handle("/frame/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("content-type", "text/html")

// Remove either of the following two calls and the iframe doesn't turn white.
fmt.Fprint(w, " ")
w.(http.Flusher).Flush()

time.Sleep(2 * time.Second) // Lots of work to do...

fmt.Fprint(w, `
<form style="background:red" method="post">
<button>submit</button>
</form>
`)
}))

log.Fatal(http.ListenAndServe(":4000", nil))
}

删除刷新或第一次写入,iframe 几乎一直保持呈现状态。为什么这种行为与顶级文档不同,我无法理解。

我不熟悉 github.com/unrolled/render。查看是否有可以延迟到最后的写入,或者您是否可以在将响应(正文)发送到网络之前对其进行缓冲。

关于iframe 在等待新页面加载时显示空白页面,而没有 iframe 则不会,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46399945/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com