473,385 Members | 1,392 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,385 software developers and data experts.

Doesn't execute Javascript when called using ajax

Hi,
What I am trying here is that from my first file I am making a call to handler.php file
//////////////////////////////FirstFile////////////////////////
[HTML]<html>
<head>
<script src='jquery-1.1.3.1.pack.js' type='text/javascript'></script>
<script type='text/javascript'>
$.post("http://practice.dev/handler.php",
function(data)
{
alert(data);
//document.write(data);
$("#test").append(data);
$("#test").html(data);
});
</script>
</head>
<body>
<div id='test'>
</div>
</body>
</html>[/HTML]

////////////////////////handler.php/////////////////
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. echo 'here';
  3. ?>
Expand|Select|Wrap|Line Numbers
  1. <script type='text/javascript'>
  2. alert('hello');
  3. </script>
  4.  
////////////////////////////////////////////////////////////

Now in firefox it is working fine means it show's alert saying hello, but when I try in IE it doesn't but if in callback function instead of using append/prepend/ html, if I use document.write(data) then it runs the javascript means it display hello message in IE 7.

Anything I am doing wrong ? IE sucks plz help help help ?
Aug 22 '08 #1
16 3165
RamananKalirajan
608 512MB
Just run that file with the help of a server. It will work fine. In IE the functionslitites are written like that. Place your code within a server and run it, it will work.

Regards
Ramanan Kalirajan
Aug 22 '08 #2
Thanks for quick reply

Run from server ? Yes, I am running it on apache server. sure there is some other catch to it.

help help
Aug 22 '08 #3
RamananKalirajan
608 512MB
Thanks for quick reply

Run from server ? Yes, I am running it on apache server. sure there is some other catch to it.

help help
Instead of append just try using insert, or update. I am not very familiar about this. Just trial and error method. this is my suggestion not the solution.

Regards
Ramanan Kalirajan
Aug 22 '08 #4
gits
5,390 Expert Mod 4TB
are both files, the html and the php on the same server and do you test from there, or from a local file?
Aug 22 '08 #5
are both files, the html and the php on the same server and do you test from there, or from a local file?
both files on same server: Yes,
Test from server: Yes

Plz help help ...
Aug 24 '08 #6
RamananKalirajan
608 512MB
both files on same server: Yes,
Test from server: Yes

Plz help help ...
Hi, wether you are using Prototype Js Framework

[HTML]$.post("http://practice.dev/handler.php",
function(data)
{
alert(data);
//document.write(data);
$("#test").append(data);
$("#test").html(data);
});[/HTML]

This code seems to be jQuery. I am confused with the URl u had given. I think the problem in ur code may be with the URL ("http://practice.dev/handler.php"). Just check that once. Just visit this site tio know more www.jquery.com

Regards
Ramanan Kalirajan
Aug 25 '08 #7
This code seems to be jQuery. I am confused with the URl u had given. I think the problem in ur code may be with the URL ("http://practice.dev/handler.php"). Just check that once. Just visit this site tio know more www.jquery.com

Regards
Ramanan Kalirajan
Hi Ramanan Kalirajan,
Thanks for reply. Yes it is jquery call [not sure but my understanding is jquery is AJAX correct me if I am wrong.]. Well URL is right and it's my local server virtual host. And yes it's returning the result from handler.php, if we concentrate on my first post with question the only problem is how IE handle javascript in firefox it's working it's executing the javascript written in handler.php but in IE it only works if I use document.write(data).
Aug 25 '08 #8
RamananKalirajan
608 512MB
Hi Ramanan Kalirajan,
Thanks for reply. Yes it is jquery call [not sure but my understanding is jquery is AJAX correct me if I am wrong.]. Well URL is right and it's my local server virtual host. And yes it's returning the result from handler.php, if we concentrate on my first post with question the only problem is how IE handle javascript in firefox it's working it's executing the javascript written in handler.php but in IE it only works if I use document.write(data).
See what u are saying I am agreeing that. But the basic thing u must understand that each browser has their own implementation logic. This we call as Browser Compatibility problem. i am working in Prototype JS Framework.

in IE, $('myBut').readAttribute('value') --> this will be working but the same doesn't works on Mozilla.

were as the code $('myBut').'value will work on both the IE and Mozilla. You have to find out the place were u had gone wrong. For me, it seems to be in the URl part u have specified http://... as it was a localhost u have to give the project workpath or some original path were the file actually placed it will work. Just try it out. i am new to JQuery.

Regards
Ramanan Kalirajan
Aug 25 '08 #9
acoder
16,027 Expert Mod 8TB
To run JavaScript, you'll need to eval it or add it to the page via script elements.

In jQuery, you'll probably have to use the $.ajax function instead of the simplified $.post function.

PS. Don't use document.write after the page has loaded - it should only be used during page load.

PPS. jQuery is not only for Ajax.
Aug 25 '08 #10
To run JavaScript, you'll need to eval it or add it to the page via script elements.

In jQuery, you'll probably have to use the $.ajax function instead of the simplified $.post function.

PS. Don't use document.write after the page has loaded - it should only be used during page load.

PPS. jQuery is not only for Ajax.
Thanks acoder, you nailed it. I was researching it and found exactly what you saying. Yes document.write will screw things up as I will lose current content of my page. True jQuery is so much more like design and other stuff. Ok now I got this idea that i have to eval(data) but now i got new problem. the content of data is something like somehtml.....morehtml .............now <script type='....>alert('hello')...</script>.....somemorehtml.

now if data is just javascript than eval is working but as I have html + javascript any suggestion how to tackle this.

Thanks
Aug 25 '08 #11
See what u are saying I am agreeing that. But the basic thing u must understand that each browser has their own implementation logic. This we call as Browser Compatibility problem. i am working in Prototype JS Framework.

in IE, $('myBut').readAttribute('value') --> this will be working but the same doesn't works on Mozilla.

were as the code $('myBut').'value will work on both the IE and Mozilla. You have to find out the place were u had gone wrong. For me, it seems to be in the URl part u have specified http://... as it was a localhost u have to give the project workpath or some original path were the file actually placed it will work. Just try it out. i am new to JQuery.

Regards
Ramanan Kalirajan
Hi Ramanan,
Thanks mate for explaining the browser compatibility. I appreciate it, but i think acoder is making more sense of my problem. Might I didn't managed to explain you my prob. I appreciate your help and effort. Still you can help me but in the direction acoder and me are going.

Thanks
Aug 25 '08 #12
RamananKalirajan
608 512MB
Hi Ramanan,
Thanks mate for explaining the browser compatibility. I appreciate it, but i think acoder is making more sense of my problem. Might I didn't managed to explain you my prob. I appreciate your help and effort. Still you can help me but in the direction acoder and me are going.

Thanks
Sure, I will be there in the forum trying to help u out.

Regards
Ramanan Kalirajan
Aug 26 '08 #13
Thanks everyone for helping me on this topic. I figured it out and come up with solution
Expand|Select|Wrap|Line Numbers
  1. if($.browser.msie)
  2.     {
  3.         try{
  4.             var re =/((<[\s\/]*script\b[^>]*>)([^>]*)(<\/script>))/i;
  5.             var match;
  6.             if(match = re.exec(data)) {
  7.                     eval(match[3]);
  8.             }
  9.         }
  10.         catch(error){
  11.             alert(error);
  12.         }
  13.     }
  14.  
Aug 26 '08 #14
acoder
16,027 Expert Mod 8TB
The reg. exp. looks OK, but why are you using browser detection?
Aug 26 '08 #15
The reg. exp. looks OK, but why are you using browser detection?
I am using browser detection because I don't want to run this bit of code for other browser as on other browser there was no problem so I don't wanted to do anything unnecessary. I want to make sure just to fix this bug without creating ripple in other things.

Cheers
Aug 28 '08 #16
acoder
16,027 Expert Mod 8TB
You should avoid browser detection if at all possible. See why. If the code works in other browsers too, then use it in all browsers.
Aug 28 '08 #17

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

Similar topics

8
by: needin4mation | last post by:
I understand this is a asp.net group, but thought I would post this here for comments. I admit I have used this post in another group, but it has less traffic. Here's to hoping I'm just blind to...
2
by: Jake Barnes | last post by:
Using javascript closures to create singletons to ensure the survival of a reference to an HTML block when removeChild() may remove the last reference to the block and thus destory the block is...
5
by: namemattersnot | last post by:
I have a SAJAX login authentication script that, if the the correct password is provided, returns "history.go(0)" to the page and I then use "eval()" on the returned data to refresh it. If the...
2
by: sgmaat | last post by:
I have create a AJAX webpage where if you press a link content is loaded into a div. NOw when this content is loaded no javascript is processed (i.e <Script>alert(..);</script>), but if the...
3
by: SM | last post by:
Hello, Im trying to access elements in my XML file using the JavaScript DOM but i'm not sure how. I use AJAX to access the XML and then use the responseXML property to access the XML file data. I...
8
by: Radu | last post by:
Hi. I have an ASP control on my page: <asp:Calendar ID="calStart" ................ Etc </asp:Calendar> and I have a Custom Validator defined as <asp:CustomValidator
10
by: paulie | last post by:
Hi, I have been experiencing an issue when trying to use AJAX to reload a DIV area using a timer of 2000ms, which contains a html page with another DIV and javascript. Scenario -------------...
3
by: pablosuk78 | last post by:
Dear coders, I'm facing a problem and after few research I found that many ppl were looking around for the same solution, hope someone more expert than me could give a good help. I created an...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.