Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
469 views
in Technique[技术] by (71.8m points)

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

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>

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

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.

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

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...