开发者

"Save" window triggered by Javascript - HOW?

开发者 https://www.devze.com 2023-01-12 15:00 出处:网络
how to triggered op开发者_如何学运维en/save window by using javascript?I assume you mean the \"Open page\" and \"Save page as...\" dialogs.

how to triggered op开发者_如何学运维en/save window by using javascript?


I assume you mean the "Open page" and "Save page as..." dialogs.

These cannot be triggered by JavaScript from within an HTML page.

You can, however, generate downloadable files from within JavaScript using the Flash-based Downloadify helper. That way, you could theoretically generate a file download in the browser that contains the current page's HTML source code.


Depending on what you are trying to save this may or may not be feasible. If you want to trigger the Save dialog to save the current page you could try document.execCommand("SaveAs") but not sure if it will work cross browser. If you want to allow the user download a file then create a link that the user clicks and the server sends the file along with the proper HTTP headers:

Content-Disposition: attachment; filename=foo.txt


Internet Explorer supports triggering Browser Commands:

document.execCommand("SaveAs");

would open the SaveAs Dialog. I am not aware of a Crossbrowser way of doing this.


Using simple Javascript I think it is no possible to open a file window.

Then I'd like to propose you the jQuery "Custom File Input" functionality.

  • http://www.daimi.au.dk/~u061768/file-input.html
  • http://plugins.jquery.com/project/custom-file

It gives you the possibility to choose a file on the client file system, it is not a real open/save window, but it can help you to build some file system interoperability function.

Download the jQuery library and then the file input plugin

http://plugins.jquery.com/files/jquery-custom-file-input.js.txt

Example:

<!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>
    <title>Untitled Page</title>
</head>
<body>

<script type="text/javascript" src="./JQuery/jquery.js"></script>
<script type="text/javascript" src="./JQuery/jquery-custom-file-input.js"></script>


<script type="text/javascript">
<!--
    $().ready(function() 
{
    $("#filewindow").file().choose(function(e, input) {
        alert("you choose: " + input.val());
    });

});
// -->
</script>


<button id="filewindow">File</button>


</body>
</html>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号