Gazow
Gazow

Reputation: 1079

Can i use javascript to prevent Loading the same script over and over?

Writing an application for a custom gallery, and all the script files are put in a resource folder inside each gallery folder-

is it possible to have a variable enabled that would prevent the page from loading its local JavaScript files but instead load from the main page's resource folder? trying to avoid having to hard-code it as well.

esentially all i really want is for my script files to be able to have a variable starting path- IE

(script src="(path)load.js" type="text/javascript")(/script)

where path is either blank "" or main main site- "http://www.site.com/resources/"

some of the files are CSS files so im not sure the class method would work well- also- is there a way to refer to the root of a site? similar to using ../ but just to get the root html path.

More Info-----

The layout i have is that each gallery made is a separate folder- (for example, photography, painting, drawings, etc- would all be separate folders). They each would contain their own resources withing their folder. this is so i can just upload 1 gallery to a site and everything would be packaged nicely. But- if im running multiple gallery on one site- as with a portfolio site, each page is loading its own set of resources, which is probably not a great idea.

The resources are - thumbnails, images, xml( which are all specific to individual gallery) but then they also each have a couple javascript files for functions, a css file, and a few images that make the gallery maneuverable(arrows and the like).

I just want to be able to have the scripts which are loaded in the header- load from the root site resource folder if there are multiple gallerys

Upvotes: 2

Views: 11336

Answers (4)

m3nda
m3nda

Reputation: 2037

If you found too much hard to handle it with javascript, you can do from server. That depends if the problem is the double call, or the Kb download resource used.

On the last case you can simply enable some cache driver to your web server, like Varnish or MemCache. Once you put a cache you have not to worry about double file loading anymore.

If you want to avoid lot of loads from the same javascript script, you can add a local counter then put 1 when it has loaded once. You will edit the initial call function to test if it's currently loaded, then avoid.

If you have lot of js files, and just wanna avoid calling the same resource twice of more, use session cookie to store the counter.

Upvotes: 0

zindel
zindel

Reputation: 1875

Perhaps you could try something like this:

in index.html (or another html file) you do:

<html>
<head>
<!-- use this if you need custom location, omit for the default one -->
<script type="text/javascript">
var GALLERY_PATH = "/gallery/";
</script>
<!-- ------------------- -->
<script type="text" src="/gallery/gallery.js"></script>
</head>
<body>
...
</body>
</html>

gallery.js:

var GALLERY_PATH = GALLERY_PATH || "http://mysite.com/default-gallery-location/";
document.write('<script type="text/javascript" src="' + GALLERY_PATH + '/js/_gallery.js"></script>')
document.write('<link rel="stylesheet" type="text/css" href="' + GALLERY_PATH + '/css/gallery.css">');
...

This way you easily include all files you need with the 1-liner and all files are loaded once. Hope it helps, of course if I understood the problem correctly ;)

Upvotes: 0

neeebzz
neeebzz

Reputation: 11538

you can put all the code under a single class name e.g. Mydata.yourvariable

and then check ..

if (Mydata) { //your script has already been loaded }

it's similar to what jQuery does with $

Upvotes: 1

Related Questions