PDA

View Full Version : Execute javascript after ajax called



MiNi.SpIdEr
04-25-2009, 02:45 PM
Execute javascript after ajax called

this is my big problem and i gone to hate AJAX :mad:

I made this code with jquery and without it and it's same thing
when i call the page by ajax it's dosent execute




<script type="text/javascript" src="dfile/js/jquery.js"></script>
<script type="text/javascript">


function checkme(){

if ($('#name').val()== '' || $('#name').val().length < 3 || $('#mail').val().indexOf('@')== -1 || $('#mail').val().indexOf('.')== -1 || $('#mail').val().length < 7 || $('#domain').val()== '' || $('#domain').val().length < 3 || $('#domain').val().indexOf('.')== -1)
{
$(adoption_it).attr("disabled", true);
}else{$(adoption_it).removeAttr("disabled");}
};


</script>
<body>


<input type=text id=name size=21 class="earea" onkeydown="checkme()"><br />
<input type=text id=mail size=21 class="earea" onkeydown="checkme()"><br />
<input type=text id=domain size=21 class="earea" onkeydown="checkme()"><br />
<input type=button id="adoption_it" value="do" onclick="" disabled><br />
<input type=button value="R" onclick="checkme()"><br />
</body>




I thing the have function in jquery it name .live() to fix this problem
but i dont know how to exploitation it here

http://docs.jquery.com/Events/live
or
http://docs.jquery.com/Frequently_Asked_Questions#Why_do_my_events_stop_working_after_an_AJAX_request.3F

please i need to fix this with with or without jquery

jscheuer1
04-26-2009, 03:25 AM
jQuery live only works with jQuery 1.3+.

Anyways, there are two basic approaches. Either you listen to the page and determine if any event that occurs qualifies for your script, or you add your event to the new content after it is imported.

In a typical non-jQuery AJAX import you eventually see something like so (from Ajax post# 23 (http://www.dynamicdrive.com/forums/showpost.php?p=190183&postcount=23)):


loadXmlHttp.prototype.stateChanged = function(){
if (this.xmlHttp.readyState == 4 && (this.xmlHttp.status == 200 || !loadXmlHttp.re))
this.el.innerHTML = this.xmlHttp.responseText;
}

Once the code is imported:


this.el.innerHTML = this.xmlHttp.responseText;

You can then initialize it:


loadXmlHttp.prototype.stateChanged = function(){
if (this.xmlHttp.readyState == 4 && (this.xmlHttp.status == 200 || !loadXmlHttp.re))
this.el.innerHTML = this.xmlHttp.responseText;
do something else here to init the just imported code
}

MiNi.SpIdEr
04-26-2009, 09:45 AM
Hi first thank you
second it's doesn't work :(

look to what i make




<script type="text/javascript" src="dfile/js/jquery.js"></script>

<script type="text/javascript">


function aClick() {

if ($('#name').val()== '' || $('#name').val().length < 3 || $('#mail').val().indexOf('@')== -1 || $('#mail').val().indexOf('.')== -1 || $('#mail').val().length < 7 || $('#domain').val()== '' || $('#domain').val().length < 3 || $('#domain').val().indexOf('.')== -1)
{
$(adoption_it).attr("disabled", true);
}else{
$(adoption_it).removeAttr("disabled");}

}


function loadXmlHttp(url, id) {
var f = this;
if (loadXmlHttp.xmlHttp){
f.xmlHttp = loadXmlHttp.xmlHttp();
f.el = document.getElementById(id);
f.xmlHttp.open("GET", url, true);
f.xmlHttp.onreadystatechange = function(){f.stateChanged();};
f.xmlHttp.send(null);
}
else alert('Your browser does not support AJAX!'); // substitute your desired request object unsupported code here
}

loadXmlHttp.xmlHttp = null; loadXmlHttp.re = /^http/.test(window.location.href);
/*@cc_on @*/ // used here and below, limits try/catch to those IE browsers that both benefit from and support it
/*@if(@_jscript_version >= 5) // prevents errors in old browsers that barf on try/catch & problems in IE if Active X disabled
try {loadXmlHttp.ie = window.ActiveXObject}catch(e){};
@end @*/
if (window.XMLHttpRequest && (!loadXmlHttp.ie || loadXmlHttp.re))
loadXmlHttp.xmlHttp = function(){return new XMLHttpRequest();}; // Firefox, Opera 8.0+, Safari, others, IE 7+ when live - this is the standard method
else if (/(object)|(function)/.test(typeof createRequest))
loadXmlHttp.xmlHttp = createRequest; // ICEBrowser, perhaps others
else {
loadXmlHttp.xmlHttp = null;
// Internet Explorer 5 to 6, includes IE 7+ when local //
/*@if(@_jscript_version >= 5)
try{loadXmlHttp.xmlHttp = function(){return new ActiveXObject("Msxml2.XMLHTTP");};}
catch(e){try{loadXmlHttp.xmlHttp = function(){return new ActiveXObject("Microsoft.XMLHTTP");};}catch(e){}}
@end @*/
}

loadXmlHttp.prototype.stateChanged = function(){
if (this.xmlHttp.readyState == 4 && (this.xmlHttp.status == 200 || !loadXmlHttp.re))
this.el.innerHTML = this.xmlHttp.responseText;

// in this code i try to add whole the code and it's same thing
// and in other way i just put the function like aClick() and also doesn't work

if ($('#name').val()== '' || $('#name').val().length < 3 || $('#mail').val().indexOf('@')== -1 || $('#mail').val().indexOf('.')== -1 || $('#mail').val().length < 7 || $('#domain').val()== '' || $('#domain').val().length < 3 || $('#domain').val().indexOf('.')== -1)
{
$(adoption_it).attr("disabled", true);
}else{
$(adoption_it).removeAttr("disabled");}

}

</script>

</head>
<body>
<br />

<div id="et3rnal"></div><br />

<input type="button" onclick="new loadXmlHttp('test.php', 'et3rnal')" value="do"><br />

</body>



in test.php file just put




<input type=text id=name size=21 class="earea" onkeydown="//checkme()"><br />
<input type=text id=mail size=21 class="earea" onkeydown="//checkme()"><br />
<input type=text id=domain size=21 class="earea" onkeydown="//checkme()"><br />
<input type=button id="adoption_it" value="do"><br />
<input type=button value="O" onclick="aClick()"><br />



can you pleas make an example

in this way it's same nothing coz it's also work in one time
just in first called