Html 针对 iframe 的 Firefox 表单正在打开新标签页

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7083325/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 10:05:59  来源:igfitidea点击:

Firefox form targeting an iframe is opening new tab

htmlformsfirefox

提问by ssell

<form method="post" target="take_the_reload">

    ...

</form>


<iframe class="hide_me" name="take_the_reload"></iframe>

My issue is as follows:

我的问题如下:

I have a form that needs to be prevented from refreshing the page it is located on when it is submitted. To combat this issue, I have been using an empty iframeas the targetfor the form. This works exactly as expected in Chrome (v12.0.742) but fails in Firefox (v6.0).

我有一个表单,需要阻止它在提交时刷新它所在的页面。为了解决这个问题,我一直使用空iframe作为target表单。这在 Chrome (v12.0.742) 中完全按预期工作,但在 Firefox (v6.0) 中失败。

What happens in Firefox, is that the iframe is opened in a new tab upon form submission, which is obviously not what I want.

在 Firefox 中发生的情况是 iframe 在提交表单时在新选项卡中打开,这显然不是我想要的。

I have found somerelatedposts, but none address my particular situation and their solutions do not work.

我找到了一些相关的帖子,但没有一个解决我的特殊情况,他们的解决方案不起作用。

Unfortunately, the work is on a proprietary system in a private network so I can not just simply provide a link.

不幸的是,这项工作是在专用网络中的专有系统上进行的,因此我不能简单地提供链接。

I have also tried using a frameas opposed to an iframeas an answer to a related topic was that using iframes in such a manner is deprecated. But the results are identical.

我还尝试使用 aframe而不是 aniframe作为相关主题的答案,iframe即不推荐以这种方式使用s 。但结果是相同的。

Also the iframeis hard-coded into the page in the sense that it is not dynamically added with JavaScript. Finally, like I said earlier, this works flawlessly in Chrome, but fails to work at all in Firefox. IE is not a concern and so any non-IE-friendly solutions are welcome!

此外,它iframe被硬编码到页面中,因为它不是用 JavaScript 动态添加的。最后,就像我之前说的,这在 Chrome 中可以完美运行,但在 Firefox 中根本无法运行。IE 不是问题,因此欢迎任何非 IE 友好的解决方案!

回答by sg3s

This might sound stupid but did you try giving the iframe an id the same as the name attribute? This seems to solve some problems relating to forms.

这听起来可能很愚蠢,但是您是否尝试为 iframe 提供与 name 属性相同的 ID?这似乎解决了一些与表单相关的问题。

<form method="post" action="link/to/post/to" target="take_the_reload">

    ...

</form>

<iframe id="take_the_reload" name="take_the_reload"></iframe>

回答by Charles

For anyone in the future looking into this, note that if you have two or more forms that target the same iframe id, Firefox will open a new tab unless the two forms have different names.Strangely enough, this isn't an issue in Chrome. So, for example, this won't work in Firefox

对于将来研究此问题的任何人,请注意,如果您有两个或多个针对相同 iframe id 的表单,Firefox 将打开一个新选项卡,除非这两个表单具有不同的名称。奇怪的是,这在 Chrome 中不是问题。因此,例如,这在 Firefox 中不起作用

<form method="post" action="link/to/post/to" target="theiframe">
<!---input stuff here---->
</form>

<form method="post" action="link/to/post/to" target="theiframe">
<!---input stuff here---->
</form>

<iframe id="theiframe" name="theiframe"></iframe>

but it will work in Chrome. If you want the code to work in both Chrome and Firefox you'd have to do something like:

但它可以在 Chrome 中工作。如果您希望代码同时在 Chrome 和 Firefox 中工作,您必须执行以下操作:

<form name="form1" method="post" action="link/to/post/to" target="theiframe">
<!---input stuff here---->
</form>

<form name="form2" method="post" action="link/to/post/to" target="theiframe">
<!---input stuff here---->
</form>

<iframe id="theiframe" name="theiframe"></iframe>

Don't know if this is a bug or Firefox is just designed that way. I'm working with Firefox 18 (latest version as of the time of this writing) and it's still a problem.

不知道这是一个错误还是 Firefox 就是这样设计的。我正在使用 Firefox 18(截至撰写本文时的最新版本),但仍然存在问题。

Hope this helps someone else; I was gnashing my teeth over this for about 2 hours before I finally figured out what the problem was.

希望这对其他人有帮助;在我终于弄清楚问题是什么之前,我为此咬牙切齿了大约 2 个小时。

Cheers!

干杯!

回答by Pawello

TIP: Don't use capital letters in id and name

提示:不要在 id 和 name 中使用大写字母

Doesn't work

不起作用

<iframe id="formSubmit" name="formSubmit">

Works

作品

<iframe id="form_submit" name="form_submit">

That was such a surprise for me!

这对我来说真是一个惊喜!

回答by Luke Madhanga

For those who may come across this problem later on, I solved it by adding a name attribute to to the iframe that was the same as the id of the iframe:

对于稍后可能会遇到此问题的人,我通过向 iframe 添加与 iframe 的 id 相同的 name 属性来解决它:

<iframe id='iframeID' name='iframeID'></iframe>

Stupid, yes, but that's the firefox likes it

愚蠢,是的,但这就是firefox喜欢它

回答by Artem Chernovol

It may sound stupid and strange, but next steps solved my problem (Firefox opened new tab when submiting in Iframe)

这可能听起来很愚蠢和奇怪,但接下来的步骤解决了我的问题(Firefox 在 Iframe 中提交时打开了新标签)

1) Use the same value for "id" and "name" Iframe attributes

1) 对“id”和“name”Iframe 属性使用相同的值

2) Do not use upper case in "id" or "name" attribute of Iframe (myIframe => my_iframe)

2) 不要在 iframe 的 "id" 或 "name" 属性中使用大写 (myIframe => my_iframe)

3) Check Iframe "src" attribute, it did not work for me when it was "about: blank", this attribute should be removed as I see

3)检查Iframe“src”属性,当它是“关于:空白”时它对我不起作用,我看到这个属性应该被删除

回答by Loophole

Another variation of this problem: I was having this same problem, and the "name" / "id" suggestions were not making any difference. I found that for me it seems to be a domain-related issue.

这个问题的另一个变体:我遇到了同样的问题,“名称”/“id”建议没有任何区别。我发现对我来说这似乎是一个与域相关的问题。

I open a page at http://mypc/app/page.aspx, with two iframes (A and B).

我在http://mypc/app/page.aspx打开一个页面,有两个 iframe(A 和 B)。

iframe A opens a new browser window to http://localhost/app/otherpage.aspx. otherpage.aspx has a form that targets iFrame B. Submitting the form opens a new window instead.

iframe A 打开一个新的浏览器窗口,访问http://localhost/app/otherpage.aspx。otherpage.aspx 有一个针对 iFrame B 的表单。提交表单会打开一个新窗口。

If I change iFrame A to open the new browser window to http://mypc/app/otherpage.aspx, then submitting the form from otherpage.aspx correctly displays the result in iFrame B.

如果我更改 iFrame A 以将新浏览器窗口打开到http://mypc/app/otherpage.aspx,则从 otherpage.aspx 提交表单会在 iFrame B 中正确显示结果。

So, at least in Firefox 47.0, it seems to be preventing cross-domain "targetting". IE11 doesn't seem to have any such concerns (at least, between localhost and the actual machine name).

因此,至少在 Firefox 47.0 中,它似乎在阻止跨域“定位”。IE11 似乎没有任何此类问题(至少,在 localhost 和实际机器名称之间)。

回答by GarfieldTheGreat

Just want to add, none of the above worked for me. I found an additional reason that made it work. Adding the id was done, and then actually adding a src= to the frame that's the same as the page it's going to show along with a ?. So my code is:

只是想补充一下,以上都不适合我。我发现了另一个使它起作用的原因。添加 id 已完成,然后实际上将一个 src= 添加到与它要显示的页面相同的框架中,并带有一个 ?。所以我的代码是:

    <iframe id="eframe" name="eframe" src="created.php?"></iframe>

Which resolves the issue, no longer opens in another window in firefox!

这解决了问题,不再在 Firefox 中的另一个窗口中打开!