Javascript - 以编程方式创建桌面链接
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13509736/
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
Javascript - create desktop link programmatically
提问by Denis Kulagin
Is there a way to programmatically create a desktop link for my webpage?
有没有办法以编程方式为我的网页创建桌面链接?
回答by AlexStack
Not from Javascript, but there is a trick to let the users do it. It is more of an interaction designtechnique rather than programming technique but I'll write it maybe someone googles this question and finds it useful.
不是来自 Javascript,但有一个技巧可以让用户这样做。它更像是一种交互设计技术而不是编程技术,但我会写它,也许有人在谷歌上搜索这个问题并发现它很有用。
Make an icon on your page with a text under it. Then ask the user to drag this icon to their desktop. The icon should be embedded in an anchor tag <a>
with the href
attribute pointing to your website. The text under the icon should be the same as the <title>
tag of your page. The browsers make an icon when the user drags an anchor link from a page to a folder, desktop or bookmark bar. The text that the browsers assign this shortcut or link is usually the <title>
of the page. Remember that the icon should be set as the background of the element so that the browser doesn't save the image instead of making a new link. It would be even better if this icon is the same as the faviconof your website.
在您的页面上制作一个图标,下方有文字。然后要求用户将此图标拖到他们的桌面上。图标应该嵌入到一个锚标记中<a>
,href
属性指向您的网站。图标下的文字应<title>
与您页面的标签相同。当用户将锚链接从页面拖动到文件夹、桌面或书签栏时,浏览器会生成一个图标。浏览器分配此快捷方式或链接的文本通常<title>
是页面的文本。请记住,应该将图标设置为元素的背景,以便浏览器不会保存图像而不是创建新链接。如果此图标与您网站的收藏夹图标相同,那就更好了。
To demonstrate this technique here is some code:
为了演示这种技术,这里有一些代码:
<p>You can make a shortcut to www.mysite.com by dragging this icon to your desktop or bookmark bar: </p>
<a href="https://www.example.com">
<div id="icon" style="background-image:url('favicon.png');width:32px;height:32px;"></div>
<div id="title">www.example.com</div>
</a>
Like a native app:
就像一个本地应用程序:
In Chrome users can go to Menu > More tools > Create application shortcut...and create a borderless shortcut to your site that looks like an app. Something similar can be done in Android, iOS and also Firefox on Android.
在 Chrome 中,用户可以转到“菜单”>“更多工具”>“创建应用程序快捷方式...”,然后为您的网站创建一个看起来像应用程序的无边界快捷方式。类似的事情可以在 Android、iOS 和 Android 上的 Firefox 中完成。
回答by Lix
This is not possible with browser scripts such as JavaScript. It is a security feature. I'm sure you can appreciate why people would not want webpages to be able to access your local file system.
这对于 JavaScript 等浏览器脚本是不可能的。这是一个安全功能。我相信您可以理解为什么人们不希望网页能够访问您的本地文件系统。
Imagine that instead of drowning in popup windows, we would be drowning in files created by spammy/scammy webpages. I'm not saying that your site is spammy or scammy, just trying to put this into perspective :)
想象一下,我们不会淹没在弹出窗口中,而是淹没在由垃圾邮件/诈骗网页创建的文件中。我并不是说您的网站是垃圾邮件或诈骗网站,只是想说明这一点:)
You could quite easily create a sort of tutorial for your users on how to create a shortcut or add your page to their bookmarks taking into consideration that users may be using different browsers, but if you cover the major browsers, Chrome, Firefox and IE, I'm sure your users will have no problem following your instructions.
考虑到用户可能使用不同的浏览器,您可以很容易地为您的用户创建一种关于如何创建快捷方式或将您的页面添加到他们的书签的教程,但如果您涵盖主要浏览器,Chrome、Firefox 和 IE,我相信你的用户按照你的指示不会有问题。
回答by Quentin
From JavaScript, running on a webpage?
从 JavaScript 运行在网页上?
No.
不。