2

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

Gazow
  • 1,019
  • 2
  • 11
  • 16
  • Could you give more details here, please? Which gallery script? Your folder structure etc. A piece of HTML might be sufficient as well. Thanks. – zindel Apr 05 '11 at 21:03
  • @zindel he is writing a CUSTOM gallery script. Structure etc and html is irrelevant he is wanting an example of how to load a script only once throughout his entire site. – Bot Apr 05 '11 at 21:05
  • did you ever find an answer to this question Gazow? I have a similar one and am also struggling to find a solution. – Daft Nov 20 '13 at 16:49
  • @Daft hmm, well, sort of. I ended up rewriting the entire application to put everything in the same folder- it runs dynamically using # in the url to seperate instances of the gallery. But to answer your question, i believe what you can do is call your scripts in the header of the file with a / at the begging to get the root site ie: ---even if it is in a sub folder, it will still call the main sites style.css, this should work with javascript files too. – Gazow Nov 21 '13 at 02:39
  • @Daft Using a variable or loading your scripts out side of the head file is a bad idea because it could take too long to load your script and then your functions wont work – Gazow Nov 21 '13 at 02:43

4 Answers4

1

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 $

neebz
  • 11,465
  • 7
  • 47
  • 64
  • 1
    hmm, im not exactly sure what your saying here – Gazow Apr 05 '11 at 20:59
  • see if this helps > http://stackoverflow.com/questions/1014203/best-way-to-use-googles-hosted-jquery-but-fall-back-to-my-hosted-library-on-goo – neebz Apr 05 '11 at 21:06
  • thanks, i think that helps a bit- ill just play around with that for a bit i suppose. – Gazow Apr 05 '11 at 21:14
0

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.

m3nda
  • 1,986
  • 3
  • 32
  • 45
0

Check out Javascript file dependencies - Selective load resource files & prevent duplicates Also check out this http://toscawidgets.org/documentation/ToscaWidgets/require_once.html

Community
  • 1
  • 1
Bot
  • 11,868
  • 11
  • 75
  • 131
0

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

zindel
  • 1,837
  • 11
  • 13