简体   繁体   English

打开一个新标签并在其中运行javascript

[英]Open a new tab and run javascript in it

I have a question about javascript and jquery. 我对javascript和jquery有疑问。

I have a scenario like this: 我有这样的情况:

  1. I have to download a file by sending a POST request to the server. 我必须通过向服务器发送POST请求来下载文件。 I did this using an invisible form which added to the HTML using jquery. 我使用不可见的表单来完成此操作,该表单使用jquery添加到了HTML中。

  2. My page of download has an implicit refresh build in by legacy code and I cannot change that. 我的下载页面有一个由旧代码生成的隐式刷新,我无法更改它。

  3. The problem is that when I try to add the invisible form to the page, the refresh happens and takes my form away. 问题是,当我尝试将不可见的表单添加到页面时,刷新会发生并且将我的表单删除。

I am thinking whether I can open a new tab, add my download invisible form, send the request and close the tab using some smart javascript. 我在考虑是否可以使用一些智能JavaScript打开一个新标签页,添加我的下载不可见表格,发送请求并关闭该标签页。 I am very new to this area, please help. 我是这个领域的新手,请帮忙。

Add a [target] attribute to your <form> and it will make the request in a new page rather than over the existing page that contains the <form> . 在您的<form>添加[target]属性,它将在新页面而不是在包含<form>的现有页面上发出请求。

[target="_blank"] would probably work, but if this ends up causing too many pages to be created, you could use a named page so that only one download page is created ( [target="downloadpage"] ). [target="_blank"]可能会起作用,但是如果最终导致创建太多页面,则可以使用命名页面,以便仅创建一个下载页面( [target="downloadpage"] )。

Alternatively you could try generating your POST request via the XmlHttpRequest object. 或者,您可以尝试通过XmlHttpRequest对象生成POST请求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM