将src属性的内容保存在localstorage中
我正在开发一个在线操作系统 我想将应用程序图标保存在本地存储中,但我不知道应该如何将 src 属性的内容保存在本地存储中并在应用程序页面上再次读出。我当前的代码会导致空白页。
I am developping an online os
And i want to save the application icons in the localstorage but i don't know how i should save the content of the src attribute in the localstorage and read it out again on the apps page. my current code results in a blank page.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您只是想保存源 URL 吗?还是图像内容?
您可以通过以下方式保存源 URL:
如果您想保存图像的内容,那么您必须对画布进行一些摆弄,使用
canvas.toDataURL()
。有关如何执行此操作的更多信息,请参阅这篇文章。Are you simply trying to save the source URL? Or the image content?
You can save source URL by something like the following:
If you want to save the contents of the image, then you have to do some fiddling around with canvas, using
canvas.toDataURL()
. See this post for more info on how to do that.据我所知没有办法做到这一点。您可以保存到本地存储的所有内容都必须由变量引用。当调用 localStorage.setItem 方法时,该值将转换为字符串格式。
As far as I know there is no way to do it. Everything you could save to local storage must be referenced by a variable. And the value would be converted into string format when call localStorage.setItem method.
图标是用户提供的还是您自己的?无论哪种情况,都请将应用程序图标转换为 base64。然后将其粘贴到localStorage中。当然,您必须对要存储的数据进行字符串化。
Are the icons user supplied or your own? In either case, convert your application icons to base64. Then stick it into localStorage. Of course you'll have to stringify the data to store.