window.open 设置block属性
时间: 2023-11-18 19:02:03 浏览: 45
在使用 window.open() 方法打开新窗口时,可以设置第三个参数为一个字符串,其中包含一些属性,如下所示:
```
window.open(url, windowName, [windowFeatures]);
```
其中,`[windowFeatures]` 参数可以包含多个属性,用逗号分隔。要设置阻止弹出窗口,可以添加 `block` 属性,如下所示:
```
window.open(url, windowName, "block");
```
这将阻止浏览器打开弹出窗口。请注意,某些浏览器可能会忽略此属性,因此您还需要采取其他措施来确保不会出现不必要的弹出窗口。
相关问题
window.open block
如果您遇到了 `window.open()` 被浏览器阻止打开新窗口的问题,可能是由于浏览器的弹出窗口拦截功能阻止了它的执行。以下是一些可能的解决方法:
1. 检查浏览器设置
请确保您的浏览器未启用弹出窗口拦截功能。在大多数现代浏览器中,您可以在设置或选项菜单中找到此设置。
2. 更改代码
如果您要在点击按钮或链接时使用 `window.open()` 打开新窗口,请尝试使用 `onclick` 或 `onmousedown` 事件,并将其与 `return false` 语句组合使用。
例如:
```
<button onclick="window.open('https://example.com'); return false;">打开新窗口</button>
```
3. 使用 JavaScript 库
一些 JavaScript 库(如 jQuery)提供了在不触发弹出窗口拦截功能的情况下打开新窗口的方法。您可以通过使用这些库来解决此问题。
例如,使用 jQuery 可以这样:
```
<a href="https://example.com" class="new-window">打开新窗口</a>
<script>
$(document).ready(function() {
$('a.new-window').click(function(event) {
event.preventDefault();
window.open($(this).attr("href"));
});
});
</script>
```
希望这些方法能够帮助您解决 `window.open()` 被浏览器阻止打开新窗口的问题。
def download_file(url, path, filename): file_path = os.path.join(path, filename) try: response = requests.get(url, stream=True, headers={'Accept-Encoding': None}) total_size = int(response.headers.get('content-length', 0)) block_size = 1024*1024 progress = 0 progress_bar = [[sg.Text('正在更新:')], [sg.ProgressBar(total_size, orientation='h', size=(20, 20), key='progressbar')]] window = sg.Window('自动开票', progress_bar) with open(file_path, 'wb') as f: for data in response.iter_content(block_size): start_time = time.time() f.write(data) progress += len(data) print(progress) # 计算下载速度和调整块大小 duration = time.time()-start_time #print(duration) # 得到下载一个块的时间 if duration > 2 and progress > block_size: # 当下载时间大于2秒以及下载进度条足够 speed = block_size/duration # 计算下载平均速度 # 将下载速度赋值给block_size if block_size > int(speed): # 如果原始块的大小大于后面计算出来的块大小,则将原始块的大小改小,如果是小于还是原来的块大小 block_size = int(speed) start_time = time.time() elif duration < 1 and progress > block_size * 2: # 当下载时间小于1秒并且已经下载超过两个块的大小时,增加块大小 block_size *= 2 start_time = time.time() #print(block_size) # 判断取消事件 window.Read(timeout=0) if progress_bar: progress_bar = window['progressbar'] progress_bar.UpdateBar(progress) sg.Popup('更新完成') print(sg.Popup) window.close() except requests.exceptions.RequestException: sg.Popup('下载更新失败,请检查网络')将以上代码切换为ftp服务器下载文件,服务器地址wei81.68.182.121,用户名为cttest,密码为123
首先,你需要用Python中的ftplib库来连接FTP服务器并下载文件。以下是修改后的代码:
```
import ftplib
import os
import time
import PySimpleGUI as sg
def download_file(url, path, filename):
ftp = ftplib.FTP('81.68.182.121')
ftp.login(user='cttest', passwd='123')
ftp.cwd(path)
file_path = os.path.join(path, filename)
try:
total_size = ftp.size(filename)
block_size = 1024*1024
progress = 0
progress_bar = [[sg.Text('正在更新:')], [sg.ProgressBar(total_size, orientation='h', size=(20, 20), key='progressbar')]]
window = sg.Window('自动开票', progress_bar)
with open(file_path, 'wb') as f:
ftp.retrbinary('RETR {}'.format(filename), f.write, block_size)
progress += block_size
print(progress)
# 计算下载速度和调整块大小
duration = time.time()-start_time
#print(duration) # 得到下载一个块的时间
if duration > 2 and progress > block_size:
# 当下载时间大于2秒以及下载进度条足够
speed = block_size/duration # 计算下载平均速度
# 将下载速度赋值给block_size
if block_size > int(speed):
# 如果原始块的大小大于后面计算出来的块大小,则将原始块的大小改小,如果是小于还是原来的块大小
block_size = int(speed)
start_time = time.time()
elif duration < 1 and progress > block_size * 2:
# 当下载时间小于1秒并且已经下载超过两个块的大小时,增加块大小
block_size *= 2
start_time = time.time()
#print(block_size)
# 判断取消事件
window.Read(timeout=0)
if progress_bar:
progress_bar = window['progressbar']
progress_bar.UpdateBar(progress)
ftp.quit()
sg.Popup('更新完成')
window.close()
except ftplib.all_errors:
sg.Popup('下载更新失败,请检查网络或FTP服务器连接')
```
注意:这里我们用到了ftplib库来连接FTP服务器,ftp.retrbinary()方法用来下载文件,ftp.size()方法用来获取文件大小。
相关推荐
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)