[英]Using Javascript to open an .xls file from a drop-down list
Cannot find anywhere how to make a drop-down list open a .xls
file. 在任何地方都找不到如何使下拉列表打开
.xls
文件的地方。
What my code is: 我的代码是什么:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> <script type="text/javascript"> function download(d) { if (d !== '') { window.open('URL goes here/' + d); } } </script> </head> <body> Excel List: <select name="download" onchange="download(this.value)" style="width:150px;" > <option value="">Select an Excel list</option> <option value="file_name1.xls">File name</option> <option value="file_name2.xls">File name</option> </select> </body> </html>
When I do this, it opens 4 additional tabs and then asked if I want to open or save the excel file. 当我这样做时,它将打开4个其他选项卡,然后询问我是否要打开或保存Excel文件。 How can I make it work by choosing the excel list and not having the 4 additional tabs open and only show to open or save the file?
如何通过选择excel列表而不打开4个其他选项卡而只显示打开或保存文件的方式使其工作?
The key to your question is the window.open()
method. 您问题的关键是
window.open()
方法。 It is documented here . 它记录在这里 。 What this method does is open a new window using the first parameter as the name of the URL to load in the new window.
此方法的作用是使用第一个参数作为要在新窗口中加载的URL的名称打开一个新窗口。 The
window.open()
method can take a value as parameter two that determines where the new browser window should be loaded. window.open()
方法可以将值用作参数2,该值确定应在何处加载新的浏览器窗口。 The default is _blank
which opens a new window (tab) which is what you are seeing. 默认值为
_blank
,它将打开一个新窗口(选项卡),您将看到该窗口。 It sounds like you may want to supply _self
to replace the current page with the selected file. 听起来您可能想提供
_self
以将当前页面替换为所选文件。
Your new code would then become: 您的新代码将变为:
function download(d) {
if (d !== '') {
window.open('URL goes here/' + d, '_self');
}
}
You just need to use '_self'. 您只需要使用'_self'。
window.open('URL goes here/' + d, '_self'); window.open('URL转到此处/'+ d,'_self');
OR 要么
window.location.href = 'URL goes here/' + d; window.location.href ='URL转到此处/'+ d;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.