最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin2浏览0评论

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

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

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

Share Improve this question edited Apr 5, 2011 at 21:17 Gazow asked Apr 5, 2011 at 20:54 GazowGazow 1,0792 gold badges11 silver badges16 bronze badges 5
  • 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 Commented Apr 5, 2011 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 Commented Apr 5, 2011 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 Commented Nov 20, 2013 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: <link type="text/css" rel="stylesheet" href="/style.css" /> ---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 Commented Nov 21, 2013 at 2: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 Commented Nov 21, 2013 at 2:43
Add a ment  | 

4 Answers 4

Reset to default 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 $

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

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

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.

发布评论

评论列表(0)

  1. 暂无评论