我可以将javascript存储在本地存储对象中并运行它吗? [英] Can I store javascript in a Local Storage Object and run it?

查看:101
本文介绍了我可以将javascript存储在本地存储对象中并运行它吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我解释一下。我正在开发一个javascript应用程序来帮助人们开发网站。我不会明确地了解它的作用,只知道它的工作原理是将它的html / inline css接口叠加在正在开发的网站上,并提供各种工具,如跟踪图像和代码缩小器。

Let me explain. I'm developing a javascript application to help people develop websites. 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.

我把它作为.js文件存储在服务器上。所有人要访问我的应用程序都是复制并粘贴一小部分html到他们的页面上使用它,如下所示:

I have it stored on a server as a .js file. 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:

<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>

然后使用jquery的.html将界面的html和内联css插入到'application'div中()函数。

The html and inline css of the interface is then inserted into the 'application' div using jquery's .html() function.

这一切都很完美。

除了一件事。加载时间。当用户开发他们的网站时,他们将不断刷新他们的页面,这导致他们不得不等待大约3秒钟(随着时间的推移非常烦人)加载应用程序的界面。

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.

当然,如果打开浏览器的缓存,则问题会消失,但如果您正在开发网站,则需要禁用缓存!这是一个难题。

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.

然后我想使用本地存储对象来保存接口的svg图形的字符串,然后将.html()这些字符串转换为内联css。这是一个精心设计的解决方法,但只有开发人员才会使用此工具。它不是最终用户的事情。它也很漂亮,但事实是,浏览器仍然需要下载脚本才能知道访问本地存储的图像!处理器速度不是瓶颈,而是带宽。

Then I thought to use local storage objects to hold strings of the interface' svg graphics, and then .html() these strings into inline 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.

初始化脚本只是将本地stroage对象中的脚本作为字符串检索,然后相应地解析然后运行它。

The initialisation script would simply retrieve the script from the local stroage object as a string, parse it accordingly and then run it.

重申我的问题,运行它是我不能做的部分!我可以通过.html(脚本)将脚本插入页面,但是我该如何运行呢?

推荐答案

虽然使用 eval(myScript)是最直接的方法,但我更喜欢这个:

While using eval(myScript) is the most direct approach, I prefer this:

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

这是使用数据uri作为源插入实际脚本标记。这样,它将显示在开发工具的资源选项卡上的脚本下。加载脚本时,将执行数据uri(您的代码)。

This is inserting an actual script tag using a data uri as the source. 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.

这篇关于我可以将javascript存储在本地存储对象中并运行它吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆