简体   繁体   中英

how to use external javascript script for variables?

I have a very long script which included many variable definitions. I am using Openlayers.js to create a webmapping application which contains about 100 layers. Each layer has to be defined as a variable and bloats the script.

I thought I could simply create a layer.js file which contains the layer definitions and reference it before the main app.js in the html start page, but this is not working.

For reference,my javascript can be seen here: http://maps.zgb.de/geoportal/app.js

The first half-page is just defining variables.

Could anyone tell me what the best-practice is in this situation?

cheers

This might be of some help http://jacklmoore.com/notes/setcountdown/

They use images in that link but im sure the same would apply to pulling in script tags.

Alternatively you could have something like this

----- HTML Page -----

function loadApp() {
   document.write('<script type="text/javascript" src="path/to/app.js"></script>');
}

---- Layers.js -----

var xyz = abc;
....
// right at the bpottom
loadApp();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM