Ajax replacing javascripts in responseText

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • vindex
    New Member
    • Jun 2008
    • 2

    Ajax replacing javascripts in responseText

    Hello everyone.

    i'm constructing a multi-language supporting webpage.
    every piece of text is put in a large javascript array which is called by the user when opening the webpage. so far so good.

    when I now use an Ajax-function to change the content of a certain <div>, which's content is in an external.php (in fact, ajax changes this php),
    i want to make this change appear directly to the viewer (me) without reloading.

    so i call again Ajax to get the content of this external.php to re-place it into the <div>.

    but the problem now: by doing this, the language-support functions are not evaluated.

    every text-piece in the source code looks like this:
    Code:
    <script>document.write(lang[int]);</script>
    where "int" is the reference to the special text-element.

    i tried to replace the <script>'s with the array's content using this:
    Code:
    body = xmlhttp.responseText;
    body = body.replace(/<script>document\.write\(lang\[(.+?)\]\)\;<\/script>/, lang[$1] );
    but it doesnt work. i tried
    Code:
    "lang[$1]"  //it replaces the string "lang[$1]" with the right integer as $1
    lang[$1]   //it says $1 not defined (of course...)
    lang["$1"]  //it replaces with "undefined" because lang["$1"] doesnt exist
    i hope you'll understand my problem =/

    is there maybe a better way to enforce the source-code from responseText to first evaluate its javascripts before placing into the <div> ?

    best wishes

    vindex
  • vindex
    New Member
    • Jun 2008
    • 2

    #2
    okay.. after what felt like hours, i made a workaround, it's prolly not the best solution, but it works for my need

    [HTML]function eval_body(text) {
    var tempmsg;
    var pattern = /<script>documen t\.write\(lang\[(.+?)\]\)\;<\/script>/;
    var ar;

    while(ar = pattern.exec(te xt)){
    tempmsg = lang[ar[1]];
    text = text.replace(pa ttern, tempmsg);
    }
    return text;
    }[/HTML]


    sorry to have bothered you,

    best regards, vindex

    Comment

    • acoder
      Recognized Expert MVP
      • Nov 2006
      • 16032

      #3
      Surely, it'd be better to return the output via PHP rather than parsing/eval-ing the response.

      Comment

      Working...