Refresh javascripts without refreshing page

9,247 views
Skip to first unread message

gius_barb

unread,
Oct 24, 2013, 11:10:59 AM10/24/13
to joomla-de...@googlegroups.com
Hello in my php projects I have a trick to refresh all scripts. That makes test quite faster, because I don't have everytime to refresh the page, and also for complex iteration I don't have to repeat same steps.

The trick is simply in 2 steps: attach all scripts to header onload , and refresh when I need to see changes , with following function.

Is there something that could help us doing the same thing? some plugin maybe or extension as fas as you know? I couldn't find anything googling

Thanks


function LoadMyJs(scriptName) {
                var docHeadObj = document.getElementsByTagName("head")[0];
                for (item in docHeadObj.children){
                   a = docHeadObj.children[item];
                    tagname = a.tagName;
                        var src= a.src;
                        if(!src) continue;
                        var same =src.indexOf(scriptName);
                        if(same !=-1){
                            a = 0;
                        }
                        if(tagname =='SCRIPT' && same!=-1) {
                            delete a;
                        }
                }
                var dynamicScript = document.createElement("script");
                dynamicScript.type = "text/javascript";
                dynamicScript.src = scriptName;
                docHeadObj.appendChild(dynamicScript);
                
                }

Fedor Vlasenko

unread,
Oct 25, 2013, 7:53:08 AM10/25/13
to joomla-de...@googlegroups.com
Good subject. But to developers to spit on you. In the ajax expansion they told that it isn't necessary.
In joomla there is no opportunity to operate order of an arrangement of scripts (head, js </body>) and their performance.
And all that can tie through php.
It is necessary to tell they mix php and js instead of dividing
Forgive for my language
var js = {
   load: function (src, callback) {
       var script = document.createElement('script'), loaded;
       script.setAttribute('src', src);
       script.setAttribute('type', 'text/javascript');
       script.setAttribute('async', true);
       if (callback) {
           script.onreadystatechange = script.onload = function () {
               if (!loaded) {
                   callback();
               }
               loaded = true;
           };
       }
       document.getElementsByTagName('body')[0].appendChild(script);
   }
};
js.load('//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js', function (jQuery) {
 alert(jQuery);
});

ModaCalcio.com - e-reall ebay

unread,
Oct 25, 2013, 9:59:40 AM10/25/13
to joomla-de...@googlegroups.com
Forgiven for sure but I didn't understand.

My aim is to refresh scripts , without refresh the page. 

This snippet is fine, but will be executed always the old version of the script not the refreshed ones. 

This is because the old version is still in memory.

I have tried http://livejs.com/ but no luck too.

I will continue trying.
Thanks




2013/10/25 Fedor Vlasenko <vlasen...@gmail.com>

--
You received this message because you are subscribed to the Google Groups "Joomla! General Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email to joomla-dev-gene...@googlegroups.com.
To post to this group, send an email to joomla-de...@googlegroups.com.
Visit this group at http://groups.google.com/group/joomla-dev-general.
For more options, visit https://groups.google.com/groups/opt_out.

Anibal

unread,
Oct 26, 2013, 7:36:11 AM10/26/13
to joomla-de...@googlegroups.com

A depency manager (RequireJS) is very usefull in this area. You can define dependencies, and execute the scripts loading task. Also, it provides a "bust" parameter where you can add the version to force script loading.

There's a tracker item about adding support on Joomla:

Regards,
Anibal

piotr_cz

unread,
Oct 28, 2013, 7:44:10 AM10/28/13
to joomla-de...@googlegroups.com
The easiest way to reload scripts is to append cacheCrusher to the src tag, ie:

script.src = script.src += '?' + (new Date()).getTime();

It will be executed once again, so make sure it does have cleanup functionality (for example detaching events) called before that.

ModaCalcio.com - e-reall ebay

unread,
Oct 28, 2013, 10:03:06 AM10/28/13
to joomla-de...@googlegroups.com
I have tried but it doens't work also with hashes 

I need to try once again with your suggestion

Also I have discovered that the live javascript edit of Chrome works quite nice, so for the moment I am going ahead with this, also if this way is quite difficult to write the code, without auto - complete suggestion and feature by netbeans

Thanks


2013/10/28 piotr_cz <pkoni...@hotmail.com>

--
Reply all
Reply to author
Forward
0 new messages