0

I have a platform that I am using at the moment that uses js, css and php files.

What I would like to be able to achieve is to force the device to reload just specific Javascript files.

If I make a change to a js file on the server I don't wish to reboot the device just to load the javascript change.

for example if in a function in one of these pages I have an alert('1'); and I wish to change this to alert('2'); I don't want to have to reload the whole device

I have tried a few javascript update scripts that add the file into the head element with a tag of the current timestamp. this doesnt seem to work, I think it downloads the file yet still continues to us the old one.

I am sorry if this is really vague. Please ask any questions and I will try and answer them

user229044
  • 232,980
  • 40
  • 330
  • 338
Liam
  • 536
  • 8
  • 23
  • 1
    what do you mean by rebooting the device to reload the files? – memical Sep 16 '14 at 18:24
  • possible duplicate of [Is there a way to "hot swap" JavaScript code within the browser?](http://stackoverflow.com/questions/207513/is-there-a-way-to-hot-swap-javascript-code-within-the-browser) – Yogu Sep 16 '14 at 18:27

3 Answers3

0

You can make a function that 'phones home' every time and gets what it needs to do.

For your example, you can make a function that makes an XmlHttpRequest, and your server would give it a value of '1' or '2'.

To expand, you can also make your server return a block of javascript code, and eval it, but bear in mind that generally using eval is a bad idea (see Why is using the JavaScript eval function a bad idea?)

Community
  • 1
  • 1
user145400
  • 1,076
  • 1
  • 12
  • 27
0

How about this, at the moment you need to choose a js file to execute, you request the code by XMLHttpRequest. The code will be returned as string. Then use eval(code) to execute it. For example, eval("alert('2')");
But, eval is evil..

Matthew Ma
  • 45
  • 6
0

Try this - worked for me. But remember to put this code as last part of body in you html.

<script type="text/javascript" language="javascript">  
    var versionUpdate = (new Date()).getTime();
    var myscript = document.createElement("SCRIPT");  
    myscript.type = "text/javascript";  
    myscript.src = "scripname.js?v=" + versionUpdate;  
    document.body.appendChild(myscript);
</script>