Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reload updated java<script> code without fully reloading the html page

I am developing a single page web application, that has many different features and forms. When developing a deep (I mean something that is not on the home page) feature, I go through this cycle:

  1. develop the code, editing classes and functions
  2. refresh the whole page
  3. clicking all the way till I get to the part that I need to test (that adds up to about a minute sometimes)
  4. testing the new code
  5. back to the (1) code editor doing updates

doing about 15 minor edits, can take a frustrating 30 minutes of repeated reloading and clicking

Is there any plugin, piece of javascript, or method, that allows to reload the updated javascript without reloading everything, so one can skip the 2. and 3. from the cycle above and continue doing live tests?

If there's no such thing, I am planning on developing a little javascript plugin that will reload the scripts, and probably with socket.io connection to a backend node.js server that will watch the files for any updates and push the load events to the browser.

So, I am interested in any idea about this, any thing that I should take into consideration when writing the plugin.

Thanks : )

like image 666
aularon Avatar asked Oct 22 '13 10:10

aularon


People also ask

Why can't I re-load Javascript files without refreshing the page?

We have some software which re-creates javascript files. We need to be able to re-load them into the DOM without refreshing the page. This means that the original javascript file (already loaded by the browser) has changed. Usually a refresh of the browser gets around this, but we can't do that due to losing state in other controls.

How do you reload a page in Java?

JavaScript provides a trendy among developers — location.reload () , which finds ways to reload the page at the current URL. The window.location object can be used for getting the current page’s URL, redirecting the browser to another page, and reloading the same page.

Can I reload the CSS without reloading the entire page?

Sometimes you don’t want to lose the changes made using the Dev Tools and simply wish to reload the CSS. Other times the CSS is so stubbornly cached that even refreshing the entire page doesn’t help. Today we will learn how to reload the CSS without reloading the entire page.

What is the difference between true and false in JavaScript reload?

If the parameter is set to true, it will reload the page from the server. If the parameter is set to false it will reload the page using the version of the page cached by the browser: The JavaScript reload () method loads the page from the cache, by default.


1 Answers

You could do something like this.

function LoadMyJs(scriptName) {
   var docHeadObj = document.getElementsByTagName("head")[0];
   var dynamicScript = document.createElement("script");
   dynamicScript.type = "text/javascript";
   dynamicScript.src = scriptName;
   docHeadObj.appendChild(newScript);
}

Call the LoadMyJs function on page load

<body onLoad="LoadMyJs()">

Then reload with the click of a button (or from your console)

<input type="button" name="reloadjs" value="Reload JavaScript" onclick="LoadMyJs('my_live_loading_script.js')">

This could be simplified using e.g jQuery

Thanks to: http://www.philnicholas.com/2009/05/11/reloading-your-javascript-without-reloading-your-page/

like image 107
subZero Avatar answered Oct 01 '22 17:10

subZero