0

Suppose I have this code:

my_script_data = document.getElementById("id_php_defer__core");
if (my_script_data == undefined) {
  Alert(request.responseText); // all looks good!
  my_script_data = document.createElement("script");
  my_script_data.setAttribute("id", "id_php_defer__core");
  my_script_data.innerHTML = 'function myinitfunc() { ' + "\n" + request.responseText + "\n" + ' }';
  to = document.head;
  if (to != undefined) {
    to.appendChild(my_script_data);
  }
}
else {
  Alert(request.responseText); // all looks good!
  my_script_data.innerHTML = 'function myinitfunc() { ' + "\n" + request.responseText + "\n" + ' }';
  eval(my_script_data.innerHTML);
}

The "request.responseText" is actually a Javascript array declaration with lots of values set.

After above code, the "myinitfunc" is called later.

(And as it should only contain "request.responseText" the global (!) array of values should be updated.)

However, while the code works on first run, subsequent runs seem to do nothing, so I am doing something wrong, but what? :)

2 Answers 2

1

<script> tags cannot be recycled. To fix that, you decided to use eval.

However (certainly in strict mode), eval does not run in the current nor global scope. As a result, the declared function does not show up.

I strongly discourage using eval for this purpose. You said that responseText contains an array. By tweaking a bit, you can use JSON to handle data.

You can also insert and remove a script element in the following way:

var s = document.createElement('script');
s.appendChild(document.createTextNode(request.responseText));
// ^ Equivalent to a "global eval"
(document.head || document.getElementsByTagName('head')[0]).appendChild(s);
s.parentNode.removeChild(s);
Sign up to request clarification or add additional context in comments.

8 Comments

Originally I inserted new <script> tags. This did not appear to work well either. (Probably because it redefined the same function over and over?) Suppose I simply append a new <script> each time simply setting values in a raw global array? Would that work? (And would the code be run when appending the new script?)
While responseText currently just contains data in an array, it could return more in the future. However, I think the problem currently is that I need to replace existing data with new data retrieved through AJAX. (JSON or no JSON, I think that problem will be the same?) I would also prefer keeping existing JS code since it has been built to accept/use: No deferred data/code load (all built into one large script file), pure JS deferred data/code load, deferred data/code load through JS + PHP at server-side. (But it is true that I currently got it to be just deferred data and not also code.)
Hi Rob, Great! I will try your solution as first thing when i get home! Thanks :)
@Tom When you inject the array in that way, the previous array will be overwritten. If you don't want that, you can create a custom function which parses input.
Hi Rob, that is what I want, replacing old values. Ideally though, I would like to be able to insert functions as well when inserting/removing script element. This does not seem to work at present. I will try again tomorrow.
|
0

Did you try to eval() every time you call myinitfunc()?

2 Comments

As from seen example, I eval when retrieving the response text. Are you suggesting I try innerHTML = eval(...) ?
I am not actually trying your code but it seems to me that it works only the first time because you actually evaluated the expression that one time, but every time after it is just some random text inside the function without any meaning. Sorry my answer might be completely incorrect but is the first thing that comes to my head.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.