473,406 Members | 2,705 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Ajax replacing javascripts in responseText

2
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:
Expand|Select|Wrap|Line Numbers
  1. <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:
Expand|Select|Wrap|Line Numbers
  1. body = xmlhttp.responseText;
  2. body = body.replace(/<script>document\.write\(lang\[(.+?)\]\)\;<\/script>/, lang[$1] );
but it doesnt work. i tried
Expand|Select|Wrap|Line Numbers
  1. "lang[$1]"  //it replaces the string "lang[$1]" with the right integer as $1
  2. lang[$1]   //it says $1 not defined (of course...)
  3. 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
Jun 23 '08 #1
2 1415
vindex
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>document\.write\(lang\[(.+?)\]\)\;<\/script>/;
var ar;

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


sorry to have bothered you,

best regards, vindex
Jun 23 '08 #2
acoder
16,027 Expert Mod 8TB
Surely, it'd be better to return the output via PHP rather than parsing/eval-ing the response.
Jun 24 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: evgenyg | last post by:
Hello ! We have the following situation - when Ajax request is sent what's being returned by the server is usually an XML (which is used for DOM updates) but sometimes it's HTML which is a whole...
1
by: geevaa | last post by:
http://www.phpbuilder.com/columns/kassemi20050606.php3 XMLHttpRequest and AJAX for PHP programmers James Kassemi Introduction: Although the concept isn't entirely new, XMLHttpRequest...
8
by: benoypaul | last post by:
I have the following javascripts which is working in IE, but not working in Firefox and opera. var xmlhttp=null; function showCustomer(str) { xmlhttp=getxmlhttp(); if (xmlhttp==null) {...
6
by: =?Utf-8?B?U2hhd24gU2VzbmE=?= | last post by:
Greetings! I was researching AJAX to provide a solution to displaying status messages while a long process executed. I found several examples online and was able to use their code to get a quick...
3
by: wendallsan | last post by:
Hi All, I've stumped myself writing an app that uses Prototype and a bit of PHP. Here is what I have: I have a custom class named Default_county_init_data that, upon initialization makes...
7
by: theS70RM | last post by:
hi guys, My code suddenly stopped working here it is: function Ajax(page,data){ page = "includes/" + page;
2
by: mndprasad | last post by:
Hi friends, Am new to AJAX coding, In my program am going to have two texbox which going to implent AJAX from same table. One box is going to retrieve the value of other and vice...
1
by: cmccarroll | last post by:
I have to two select boxes with values and two javascripts which call server-side scripts. The first select box will write to the first <div id='txtHint1'></div>, but when I intoduce a second <div...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.