xyn Posted January 5, 2008 Share Posted January 5, 2008 Hey. I have an ajax code to run a small chat client. The client works perfectly. although when i try adding an alert to display instantly, I have problems. Basically php is printing "<script>alert('Notice:".$msg."');</script>"; then that is added along with the rest of the coding, then updated in the DIV using document.getElementById('myDiv').innerHTML =ajx; I debugged my SQL. and it works perfectly. its just the Javascript which wont work. Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted January 6, 2008 Share Posted January 6, 2008 That code doesn't tell me much. Can you post your code? Quote Link to comment Share on other sites More sharing options...
mainewoods Posted January 23, 2008 Share Posted January 23, 2008 document.getElementById('myDiv').innerHTML =ajx; --you can not execute javascript by innerHTML'ing it. It just doesn't work and I don't think it works on any browser. If you want to return a value from php down the ajax pipe and then alert it, you have to divide out the value from the other stuff returned using javascript functions and then alert it. Which means you will have to separate it or mark it some way on the server side so that the client side can separate it out. You could use json, or just surround it with some special characters server side: <?php if ($sendmessage) { // if I have a special message set, I will send it echo '<!--**alertme**' . $sendmessage . '**-->'; } ?> --in the case above I would use javascript regular expression search functions on the ajax return value to find strings beginning with the opening html comment tag plus 2 *'s plus 'alertme**' and ending with 2 *'s and the ending html comment tags. Quote Link to comment Share on other sites More sharing options...
priti Posted January 23, 2008 Share Posted January 23, 2008 Hey. I have an ajax code to run a small chat client. The client works perfectly. although when i try adding an alert to display instantly, I have problems. Basically php is printing "<script>alert('Notice:".$msg."');</script>"; then that is added along with the rest of the coding, then updated in the DIV using document.getElementById('myDiv').innerHTML =ajx; I debugged my SQL. and it works perfectly. its just the Javascript which wont work. I will suggest you to debugg the javascript in firebug in firefox. kindly check weather you are receiving the expected output in your javascript by alerting the response in js. Regards Quote Link to comment Share on other sites More sharing options...
mainewoods Posted January 23, 2008 Share Posted January 23, 2008 document.getElementById('myDiv').innerHTML ='<script>alert("hello");<' + '/script>'; --you cannot execute javascript by innerHTML'ing it! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.