PHP var->Javascript->PHP -


I am working on a project that has the following problem:

returns the database ( Via php)) is an array filled with a list of javascript files. This variable is stored in the $ array (php) . I need to extract those source files with the php (foreach loop) and load them via javascript. Is:

  document.write ("& lt ;? php foreach ($ js_files as filename) {if (trim ($ filename) <>" ") { Echo '' script type = \ 'text / javascript \' src = $ filename> & lt; / script & gt; \ n ';}}? & Gt; ");   

The problem is that it loads some files, but goes wrong with the first one (a Google API file). Does anyone have a sollution for this? Or in any thought I have the direction of seeing.

This is a bad idea at many levels, but can be solved very easily. But first: 1) Never use document.write () . It is better to use dynamically to load a script:

  var script = document.createElement ('script'); // Script a script element. Rc = 'javascript.js'; // path for the src file // Now get the body element and add new script elements document. GetElementsByTagName ('body') [0] .appendChild (script);   

2) Loading such scripts probably will not work if they have to load in a particular order, because downloading dynamic scripts is asynchronous (non-determinative)

3) Generally, you should add your js files to reduce the http request, you can use a tool to create an automated device .

4) If you really want to actually load those scripts dynamically, then you use the filename AJAX to specify in (1) The process can be used:

  // depends on jQuery, but vanilla can be written if necessary. $ (. 'MyPhp.php', resp) {var arr = resp.split (','); // Returns the splits strings, while the comma var i = arrow.length; // iterate through the result, while (I--) {// above (1) process)}};   

Note that this is still an unprotected issue. If you need a sequential dynamic load then look something like

Comments

Popular posts from this blog

java - ImportError: No module named py4j.java_gateway -

python - Receiving "KeyError" after decoding json result from url -

.net - Creating a new Queue Manager and Queue in Websphere MQ (using C#) -