Load remote js file

Login or register to post comments
6 replies [Last post]
agsel's picture
Offline
Joined: 04/02/2008

Has anyone tried to load remote js file (from another server) with javascript function qc.loadJavaScriptFile? Or have you tried to use QControlBase::$strJavaScripts property to include javascript files? The latter should use qc.ladJavaScriptFile when control is loaded via ajax.

I have a dialog box, which should load js from another server and use this js to do stuff. If I see ajax response, there's something like:

qc.loadJavaScriptFile("http://anotherserver.com/js/file.js", function() {alert("a");});

It just has to load js file and alert 'a' afterwards. But it just starts loading something and won't alert. If I have some other alert outside of this function, then this part is executed correctly. (Given code is generated manually using QApplication::ExecuteJavaScript('qc.loadJavaScriptFile(....)');

By default, if you use $strJavaScripts, every JS on the page will be included inside qc.loadJavaScriptFile (as a callback function as second parameter). So, if I add remote javascript file to qcontrol, nothing is executed (even my dialog box will not be shown).

Anyone experienced something like that?

Edit: There's no difference, whether to use local or remote js file to include. I made a simple example, which I think does not work as expected. Correct me, if I'm wrong (button which includes js file, should alert both "test.js" and "clicked" - or ?).

agsel's picture
Offline
Joined: 04/02/2008

Note, that this works somewhat ok in IE. At least it executes other js besides loaded "test.js"

agsel's picture
Offline
Joined: 04/02/2008

As the first line of "loadJavaScriptFile" function is:

strScript = qc.jsAssets + "/" + strScript;

it's pretty clear that my stuff didn't work :)

But should we add a possibility to load remote (non-asset-directory) js files? I have adjusted this function to meet my requirements.

if (strScript.substr(0, 7).toLowerCase() != 'http://') {
strScript = qc.jsAssets + "/" + strScript;
}

Although it would be nice, if file is not found, other js would be executed.

alex94040's picture
Offline
Joined: 11/06/2008

I think this indeed would be pretty useful - there are lots of sources one can load the jQuery library from, for example. Allowing controls to specify javascripts from the outside is a fine idea, I'd welcome it to the core. When you have it completely figured out, can you create a ticket with a patch, please?

agsel's picture
Offline
Joined: 04/02/2008

Well, my last given code did it for me. It works for javascript files, which start with 'http://'. But before we create a patch, I wanted to discuss, whether we need to limit javascript loading function to be able to load only from assets directory. With plugins and stuff, should all js files need to be in the same directory?
I tend to use separate directory for my custom js files (though there is not really any benefit for it compared to assets/js). So, I just thought, that I can include for example '/scripts/jquery/some.js' file. But it comes out, that assets folder is added before my script path. So it becomes something like '/assets/js/scripts/jquery/some.js'. It might be ok, but we should at least mention it somewhere in doc.

So, to make it short, I have one question:
Do we really want to limit "loadJavaScriptFile" function in qcodo.js so that it can only read scripts from local "assets" directory? And is it even possible/reasonable with the new plugin structure (Sorry, haven't had much time to investigate it)? What are the pros/cons of this?

After that I could create a patch.

alex94040's picture
Offline
Joined: 11/06/2008

I think it'd be fine do define a new loadExternalJavaScriptFile() function in Javascript that would allow you to get external scripts; plus, it'd be very easy to code-review - this explicit function call would mean that this code is "less safe" than the code that loads js from the local assets folder.

I think it's fine to keep the loadJavaScriptFile() function's behavior as-is - maintaining backward compatibility seems important to me.

What you're describing would work just fine with the new plugin structure - the only thing that wouldn't work fine is attempting to place a .js file under /includes. /includes will soon be protected with an .htaccess file that doesn't allow HTTP access to any file under it. /assets folder, on the contrary, is meant to be web-accessible.

agsel's picture
Offline
Joined: 04/02/2008

Additional function is ok. But it might be too complicated for new users. The thing is, that if you use strJavaScripts property in QControlBase, qcubed will do the following for you:

qc.loadJavaScriptFile("http://www.example.com/my.js", function() {
// your other js, also qc calls are done here
});

The point is, that only after the js file is loaded, js is executed (as some js may depend on external js file. And if you have more js files, they are loaded in the same manner inside each other. So, I think we should have an easier option to include js for developer.

May-be we can add additional array of js files (something like QControlBase::$strExternalJavaScriptArray), which will automatically be loaded through qc.loadExternalJavaScriptFile function in JS (and callback is done automatically for user). Of course, experienced users can use it directly if they want.

Thoughts?