Executie cod javascript preluat prin Ajax

Discutii si intrebari legate de scripturi si functii JavaScript, jQuery si Ajax, cod JavaScript in general.
andras
Mesaje: 430

Executie cod javascript preluat prin Ajax

Salut,
Trimit cu ajax() catre un fisier

Cod: Selectaţi tot

my_ajax1("updateprogramare.php",data_json,"post");
al carui rezultat il pun intr-un div in fisierul de baza (prog_client.php). Tot in updateprogramare.php am alert-uri:

Cod: Selectaţi tot

<?php
echo  '  <script> '; 
echo'    alert("Rezervare reusita ! Va multumim.")';
echo ' </script>';
?>
Cum fac sa-mi apara doar mesajul "Rezervare reusita ! Va multumim."? In prezent apar toate: "<script>alert("Rezervare reusita ! Va multumim.")</script>".
Multumesc.

MarPlo Mesaje: 4343
Salut
Cred ca iti poate fi de folos functia parseScript() din acest exemplu:

Cod: Selectaţi tot

<script>
// this function create an Array that contains the JS code of every <script> tag in parameter
// then apply the eval() to execute the code in every script collected
function parseScript(strcode) {
  // function from: https://coursesweb.net/ajax/
  var scripts = new Array();         // Array which will store the script's code
  
  // Strip out tags
  while(strcode.indexOf("<script") > -1 || strcode.indexOf("</script") > -1) {
    var s = strcode.indexOf("<script");
    var s_e = strcode.indexOf(">", s);
    var e = strcode.indexOf("</script", s);
    var e_e = strcode.indexOf(">", e);
    
    // Add to scripts array
    scripts.push(strcode.substring(s_e+1, e));
    // Strip from strcode
    strcode = strcode.substring(0, s) + strcode.substring(e_e+1);
  }
  
  // Loop through every script collected and eval it
  for(var i=0; i<scripts.length; i++) {
    try {
      eval(scripts[i]);
    }
    catch(ex) {
      // do what you want here when a script fails
    }
  }
}

// string with javascript scripts
var str_script = '<scrip'+'t>alert("Script 1");</scri'+'pt>\
<scri'+'pt>var str2 = "Script 2"; alert(str2);</scri'+'pt>';

// parse and executes the scripts from str_script
parseScript(str_script);
</script>