0

I previously had this problem with an external file not running.

I fixed this by loading the file dynamically instead...i.e. creating the element in JavaScript and linking to it via the .src attribute ( did this instead of using a scrip tag in the html ). Do I need to do something similar here?

Here is the embedded JavaScript I need to run that is inserted via .innerHTML of the body. Currently it is just "dead code" in my page. Note the json I'm passing in as the second variable to the set function. This is PHP composed and passed to the client via ajax POST.

<script type='text/javascript'>new Arc.Shared().set( 'tweet_data', [{"id":"1","0":"1","picture":"0","1":"0","name":"Test Account","2":"Test Account","tweet":"Hi","3":"Hi","time":"1338559048","4":"1338559048"},{"id":"1","0":"1","picture":"0","1":"0","name":"Test Account","2":"Test Account","tweet":"hi","3":"hi","time":"1338558809","4":"1338558809"},{"id":"1","0":"1","picture":"0","1":"0","name":"Test Account","2":"Test Account","tweet":"<a class=\"bookmark_tweet\" target=\"_blank\" href=\"http:\/\/ebay.com\">ebay <\/a>","3":"<a class=\"bookmark_tweet\" target=\"_blank\" href=\"http:\/\/ebay.com\">ebay <\/a>","time":"1338504456","4":"1338504456"},{"id":"1","0":"1","picture":"0","1":"0","name":"Test Account","2":"Test Account","tweet":"foo","3":"foo","time":"1338504225","4":"1338504225"},{"id":"1","0":"1","picture":"0","1":"0","name":"Test Account","2":"Test Account","tweet":"foo","3":"foo","time":"1338504222","4":"1338504222"},{"id":"1","0":"1","picture":"0","1":"0","name":"Test Account","2":"Test Account","tweet":"foo","3":"foo","time":"1338504220","4":"1338504220"},{"id":"1","0":"1","picture":"0","1":"0","name":"Test Account","2":"Test Account","tweet":"foo","3":"foo","time":"1338504217","4":"1338504217"}] );</script>

Notes:

Alternative to Eval Snippet:

var myCode = 'alert("Howdy?");';
 var myFucn = new Function(myCode);
 myFucn();
Kev
  • 118,037
  • 53
  • 300
  • 385
  • possible duplicate of [Executing – Quentin Jun 01 '12 at 21:19
  • Please don't edit your question this dramatically when it's already got answers. Post a new question instead. – Michael Petrotta Jun 12 '12 at 19:27

2 Answers2

1

If you want to run AJAX injected JS, you should call "eval()" on it (for security reasons). Another possibility is to load it via a <script src='[url to your JS]'></script> tag.

fmgp
  • 1,638
  • 17
  • 17
  • You must have a server-side component providing your script – fmgp Jun 01 '12 at 18:49
  • You will have to use the "eval([your js as text here])" function – fmgp Jun 01 '12 at 18:52
  • the eval must be called outside from the JS (deactivated) you load with AJAX. The best way is to make the call just after you retreive your embedded js from AJAX as a callback – fmgp Jun 01 '12 at 18:55
  • script tags can't have an id, but it will be easier to grab all scripts with "document.getElementsByTagName('script')" – fmgp Jun 01 '12 at 19:03
  • For your problem, I don't find anything better. – fmgp Jun 01 '12 at 19:08
0

Another way per Why doesn't embedded JavaScript execute when inserted in response to an ajax call?

Here is the append child method...that take ajax responseText and appends it into the DOM.

function onSuccess() {
    src = document.createElement('script');
    src.innerHTML = this.responseText;
    document.body.appendChild(src);
}

Third way is to have an external element trigger it per here:

Executing <script> elements inserted with .innerHTML

Community
  • 1
  • 1