Force page load to always reload external JS library

M

Mark Anderson

Sorry if this is borderline JS vs. HTML. I ask here as the
success/failure if the resulting JS is key.

I've a small external .JS library that allows a non code-savvy user to
occasionally update a list of values that are read as a JS array and
used to populate the <option>s of a <select> in the calling page's form.

Anyway, is there a way by which I can ensure, regardless of the user's
browser settings, that the JS library is not used from cache but re-read
each time, to ensure the up to date list is used?

If not, I might as well put the JS array code back in the calling page,
especially as the split is for ease of use by the page's owner, albeit
with increased change of editing errors. A database solution, etc., is
inappropriate here.

TIA,

Mark
 
R

Randy Webb

Mark said:
Sorry if this is borderline JS vs. HTML. I ask here as the
success/failure if the resulting JS is key.

I've a small external .JS library that allows a non code-savvy user to
occasionally update a list of values that are read as a JS array and
used to populate the <option>s of a <select> in the calling page's form.

Anyway, is there a way by which I can ensure, regardless of the user's
browser settings, that the JS library is not used from cache but re-read
each time, to ensure the up to date list is used?

If not, I might as well put the JS array code back in the calling page,
especially as the split is for ease of use by the page's owner, albeit
with increased change of editing errors. A database solution, etc., is
inappropriate here.

You dynamically create the script tag with a unique paramater to it.

<script type="text/javascript">
document.write('<script type="text/javascript"
src="myFile.js?' + new Date().getTime() + '">
<\/script>');
</script>

It is split across lines for readability only.
The idea is that the parameter in the file name should always be unique
since you are appending the current time to the filename. Since the
filename is unique, it can not be retrieved from the cache.
 
M

Mark Anderson

Randy Webb said:
You dynamically create the script tag with a unique paramater to it.

<script type="text/javascript">
document.write('<script type="text/javascript"
src="myFile.js?' + new Date().getTime() + '">
<\/script>');
</script>

It is split across lines for readability only.
The idea is that the parameter in the file name should always be
unique since you are appending the current time to the filename. Since
the filename is unique, it can not be retrieved from the cache.

I get the idea but it doesn't work as the browser can't find the
referenced JS file as its filename is missing the date info added by the
dynamic reference. I've tested the solution - IE & Firefox both fail.

I used (lines may wrap):
~~~HTM~~~
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled</title>
<script type="text/javascript">
document.write('<script type="text/javascript" src="myFile.js?' + new
Date().getTime() + '"><\/script>');
</script>
</head>
<body>
Text Line 1 in HTM file source<br><br>
<script type="text/javascript">
writeMe();
</script>
</body>
</html>
~~~End HTM~~~
~~~JS~~~
function writeMe(){
document.write('Text Line 1 from JS file source<br><br>Text Line 2
from JS file source.')
}
~~~End JS~~~

Thanks,

Mark
 
L

Lee

Mark Anderson said:
I get the idea but it doesn't work as the browser can't find the
referenced JS file as its filename is missing the date info added by the
dynamic reference. I've tested the solution - IE & Firefox both fail.

I used (lines may wrap):
~~~HTM~~~
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled</title>
<script type="text/javascript">
document.write('<script type="text/javascript" src="myFile.js?' + new
Date().getTime() + '"><\/script>');

The server will ignore the "?" and anything following it when it looks
for the .js file. If it's failing to find it, it's because you don't
have a file named "myFile.js" in the base directory.
 
M

Mark Anderson

Lee said:
Mark Anderson said:
The server will ignore the "?" and anything following it when it looks
for the .js file. If it's failing to find it, it's because you don't
have a file named "myFile.js" in the base directory.

Yes, apologies - the problem was a naming error. All working!

Thanks everyone,

Mark
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top