Jump to content

cant use javascript in ajax


traxion

Recommended Posts

hey allive tryed to made myself a site with ajax (not the football club:P)everythink works normal but i cant reload the page's automaticallybefore i just ajax on the website id used meta refresh but that dont work in with the ajaxnow im trying to reload the page true a js function but that also didnt work. Im now debugging and i see that i even dont do the function automatically. when i press a button i can use functionsfunc_javascript.js function checkAll(){alert("sdf");var t=setTimeout("alert('5 seconds!')",5000);}algemeen-login.php<script type="text/javascript"> checkAll(); alert("sdf");</script>what im i doing wrong??

Link to comment
Share on other sites

ow sorry, the website is ******and u can login with username: test1pass: test1the page must reload when u are login.at the moment im ive delete the reload pease of the code because i cant call a function automatically when u got any more questions please ask them

Link to comment
Share on other sites

<span class="tekstheader">Welkom test1! Je bent succesvol ingelogd!</span><br>Je wordt automatisch door gestuurd.<script type="text/javascript"> checkAll(); alert("sdf");</script> is the script when im login, u can see after the text i call the function (checkAll), the alertbox also doesn't work, way i dont have any idee way?

Link to comment
Share on other sites

If you're trying to pull in Javascript code using AJAX, you're going to need to use eval to run it. When you add an AJAX response to a page it doesn't get executed, it's just text. If you want to execute any code you will need to isolate the Javascript portion of the code and use eval. eval will give an error if you try to use it on any non-Javascript code, including HTML.

Link to comment
Share on other sites

thnx for the tip, i will go check it outi think u mean http://www.w3schools.com/jsref/jsref_eval.asp this functionsfor a test i have try<script type="text/javascript"> eval("alert('Hello world')");</script> but i still dont get any pop-up or error from the javascript. i have also put it into the index.php (just for testen) and then i get a pop-up. so i think im doing wrong but what?

Link to comment
Share on other sites

oke.. i think not everybody understands mine problem. i will try to explain it a time again.im making a site (cammora) with ajax. i've create mine own ajax function (http://cammora.be/functions/func_javascript.js) and a way to call it is<a onclick="verstuur('algemeen-signup.php')"> now im having a problem using javascript in the ajax, i've been told that ajax disable javascript as protection. U can there is a function called eval() where u can force the javascript to be executed.so i've tried to use the function http://www.w3schools.com/jsref/jsref_eval.asp , but to bad it all isn't working for me.does anybody can tell me way it isn't working or better tell me the solution?

<div id="main">		<table border="0" width="600">	text and the content of the page</table><script type="text/javascript">eval("x=10;y=20;document.write(x*y)");document.write("<br />");document.write(eval("2+2"));document.write("<br />");var x=10;document.write(eval(x+17));document.write("<br />");</script><script src="http://www.google-analytics.com/urchin.js" type="text/javascript"></script><script type="text/javascript">_uacct = "UA-3036431-2";urchinTracker();</script>										</div>

already thnx for the replays

Link to comment
Share on other sites

Well, before I start searching for more errors, there's already this case:You're calling the function with one parameter:verstuur('algemeen-signup.php')and the function needs four:function verstuur(page, type, name, value)

Link to comment
Share on other sites

thats correct. but when u don't need to parameters u can leave them empty, just like in PHP. I didn't get any errors, in IE or FF so i thought it was oke here is a complete function call where all the parameters are filledverstuur('misdaad-route66.php', 'post', new Array('submit1', 'bestuurder', 'kogels'), new Array('dummy', getElementById('bestuurder').value, getElementById('kogels').value))

Link to comment
Share on other sites

You didn't understand what I was saying. This is the function you have to handle the ready state changing:

function StatusGewijzigd() { 	if (xmlHttp.readyState==4) document.getElementById("content-main").innerHTML=xmlHttp.responseText;}

All you do is take the response content and write it to the page. That doesn't work with Javascript. You need to eval the code instead. This would work to execute java script:

function StatusGewijzigd() { 	if (xmlHttp.readyState==4) eval(xmlHttp.responseText);}

But pay attention to what I'm about to say. That only works if the response text is Javascript code. So if this is the response text:alert('test');Then the above code will work. If the response text has anything other then Javascript code, eval will fail. This will fail:<script type="text/javascript">alert('eval');</script>That fails because of the script tags. Script tags are not Javascript code, they are part of HTML. The eval function does not evaluate HTML code. Likewise, eval will fail if it sees any other HTML tags.So, if your response text is HTML only then you can use the innerHTML property to put it on the page. If the response text is Javascript only then you can use eval to execute the code. If the response is both HTML and Javascript, then you will need to build a very complex piece of code to find the Javascript embedded inside the HTML and execute it independently of anything you do with the HTML.

Link to comment
Share on other sites

oke.. thnx for the clear explanationi don't have right now the time to test it so i've going to ask itis it possible to first the html part and then the eval/ javascript part so that on the page in the end both are loaded?i think of not but just asking for sureeverybody thnx for the replays:)

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...