简体   繁体   English

我可以将javascript存储在本地存储对象中并运行它吗?

[英]Can I store javascript in a Local Storage Object and run it?

Let me explain. 让我解释。 I'm developing a javascript application to help people develop websites. 我正在开发一个javascript应用程序来帮助人们开发网站。 I wont explicitly go into what it does, just know that it works by superimposing its html/inline css interface over the website that's being developed and offers a variety of tools such as tracing images and code minifiers. 我不会明确地了解它的作用,只知道它的工作原理是将它的html / inline css接口叠加在正在开发的网站上,并提供各种工具,如跟踪图像和代码缩小器。

I have it stored on a server as a .js file. 我把它作为.js文件存储在服务器上。 All people have to do to access my application is copy and paste a small bit of html onto their page to use it, like this: 所有人要访问我的应用程序都是复制并粘贴一小部分html到他们的页面上使用它,如下所示:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="www.example.com/application.js">
<div class="application"></div>

The html and inline css of the interface is then inserted into the 'application' div using jquery's .html() function. 然后使用jquery的.html()函数将接口的html和内联css插入到'application'div中。

It all works perfectly. 一切都很完美。

Apart from one thing. 除了一件事。 The load time. 加载时间。 As a user develops their site they will be continually refreshing their page, and this results them having to wait about 3 seconds (very annoying as time goes on) for the application's interface to load. 当用户开发他们的网站时,他们将不断刷新他们的页面,这导致他们不得不等待大约3秒钟(随着时间的推移非常烦人)加载应用程序的界面。

Of course, if the browser's cache is turned on, the problem dissappears, but if you're developing a site you're going to want to have your cache disabled! 当然,如果打开浏览器的缓存,则问题会消失,但如果您正在开发网站,则需要禁用缓存! It's a conundrum. 这是一个难题。

Then I thought to use local storage objects to hold strings of the interface' svg graphics, and then .html() these strings into inline css. 然后我想使用本地存储对象来保存接口的svg图形的字符串,然后将.html()这些字符串转换为内联css。 It's an elabourate workaround, but only developers would use this tool. 这是一个精心设计的解决方法,但只有开发人员才会使用此工具。 It isn't an end user thing. 它不是最终用户的事情。 And it works beautifully too, but the thing is, the browser still needs to download the script in-order to know to access the locally stored images! 它也很漂亮,但事实是,浏览器仍然需要下载脚本才能知道访问本地存储的图像! Processor speed isn't bottlenecking it, it's bandwidth. 处理器速度不是瓶颈,而是带宽。

So I was thinking storing the script itself in a local storage object, and having a tiny initialisation script to run it. 所以我在考虑将脚本本身存储在本地存储对象中,并使用一个很小的初始化脚本来运行它。

The initialisation script would simply retrieve the script from the local stroage object as a string, parse it accordingly and then run it. 初始化脚本将简单地从本地stroage对象中检索脚本作为字符串,相应地解析它然后运行它。

To reiterate my question, running it is the part I can't do! 重申我的问题,运行它是我不能做的部分! I can insert the script onto the page via .html(script), but then how do I go about running it? 我可以通过.html(脚本)将脚本插入到页面中,但是我该如何运行呢?

While using eval(myScript) is the most direct approach, I prefer this: 虽然使用eval(myScript)是最直接的方法,但我更喜欢这样:

var script = document.createElement('script');
script.src = 'data:text/javascript,' + encodeURI(myScript);
script.onload = function() {
  //optional callback
};
document.body.appendChild(script);

This is inserting an actual script tag using a data uri as the source. 这是使用数据uri作为源插入实际脚本标记。 This way, it will appear under "scripts" on the resources tab of the dev tools. 这样,它将显示在开发工具的资源选项卡上的“脚本”下。 When the script loads, the data uri (your code) will be executed. 加载脚本时,将执行数据uri(您的代码)。

Here are two methods: 这有两种方法:

Method 1: eval() 方法1: eval()

eval(localStorage.myCode);

Method 2: the Function constructor 方法2: Function构造函数

new Function(localStorage.myCode)();

Here are the jsfiddle demos showing that they all work: 以下是jsfiddle演示,显示它们都有效:

Method 1: http://jsfiddle.net/markasoftware/3BkAV/ 方法1: http//jsfiddle.net/markasoftware/3BkAV/

Method 2: http://jsfiddle.net/markasoftware/j6JAz/ 方法2: http//jsfiddle.net/markasoftware/j6JAz/

You just need to use the eval function of JavaScript. 您只需要使用JavaScript的eval功能。

You can end up writing something like the following: 您最终可以编写如下内容:

localStorage.myCode = "alert('just a test')";
eval(localStorage.myCode);

You can check if your code is stored like this: 您可以检查您的代码是否存储如下:

if (!localStorage.myCode) {
    // local storage code not exists, load from somewhere else
} else {
    // perform the eval using the code from local storage
}

I tested this code in Chrome and works. 我在Chrome中测试了这段代码并且可以使用。 The only problem I see is that you are forcing your users to use only browsers that support local storage. 我看到的唯一问题是您强制用户仅使用支持本地存储的浏览器。

Here is the browser support: 这是浏览器支持:

  • IE 8+ IE 8+
  • FireFox 3.5+ FireFox 3.5+
  • Safari 4+ Safari 4+
  • Chrome 4+ Chrome 4+
  • Opera 10.5+ Opera 10.5+

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Javascript将cookie存储在本地存储中? - How can I store a cookie in local storage with Javascript? 如何在本地存储中存储 JavaScript 变量? - How can I store a JavaScript variable in local storage? 我怎样才能将对象存储在本地存储中的角度? - How can i store object in local storage in angular? 如何将带有方法的JavaScript对象存储在本地存储中? - How to store a JavaScript object with methods in local storage? 我可以每次都在本地存储中存储具有相同属性的对象数组,即使我在 javaScript 中刷新页面也会保留吗? - can I store array of object in local-storage with the same property every time which is gonna stay even if I refresh the page in javaScript? 如何将数据存储在 JavaScript 阵列中,以便可以将其存储在本地存储中? - How can I store data in a JavaScript Array so it can be stored in local storage? 我可以存储在 LOCAL STORAGE 但刷新页面后列表仍然消失了? 带有 Javascript 的基本待办事项列表应用程序 - I can store in LOCAL STORAGE but the lists are still gone after refreshing the page? basic Todo list app with Javascript 有没有办法可以将图像和字符串添加到数组并存储在本地存储中并使用 JavaScript 显示它们? - is there a way I can add images and strings to an array and store on local storage and display them using JavaScript? 如何在本地存储中存储和检索许多JavaScript原语 - How can I store and retrieve many javascript primitives in and from local storage 如何在本地存储中以及从本地存储中存储和检索许多JavaScript变量 - How can I store and retrieve many javascript variables in and from local storage
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM