Link to home
Start Free TrialLog in
Avatar of jaylab2
jaylab2

asked on

How can i execute a javacript after success of another

Hello i do have  these functions

function egnyt_facebook(pixel,val, cny) {
  var cd = {};
  cd.value = val;
  cd.currency = cny;
  _fbq.push(['track',pixel, cd]);
}
//combine facebook and google
function egnyt_track(pixel, val, cny,type ,process,name){

	egnyt_facebook(pixel, val , cny);
	_gaq.push(['_trackEvent',type, process, name]);
       window.location='';


}

Open in new window


I do need to call this function egnyt_track(pixel, val, cny,type ,process,name) on onclick event and need to be sure that _gaq.push(['_trackEvent',type, process, name]); is executed after egnyt_facebook(pixel, val , cny); this and after all these executed i do need to redirect to certain page and if i need to know if i can delay a normal <a href=""> combined with onlclick event not to redirect before executing the function anyone can help me sorting this out with an example based on the script above.
Avatar of OriNetworks
OriNetworks

To my knowledge, javascript will only reach the next line if the previous line completed so _gaq.push will only run after egnyt_facebook. As for changing a <a href=""> to delay the navigation I don't think that's possible by default behavior. It may work if you are able to use
<a href="#" onclick="completeBeforeRedirect('http://google.com')">test</a> 

Open in new window

you could then have a javascript function to run code before changing window.location
function completeBeforeRedirect(myUrl) {
//do stuff
//now go to url
window.location = myUrl;
}

Open in new window

Avatar of jaylab2

ASKER

So what's callback function is used for ? i am new into this !
I think I should first clarify that there are situations where javascript would not be synchronous and I only know of that to be the case with ajax, otherwise javascript runs line by line and only after the previous line completes. a callback function is a technique and the same thing can be accomplished without using a callback.
for example with a callback you could do
function doMoreStuff(myString) {
  alert(myString)
}
function processMyData(func) {
//do something
   func('this is a test');
}
//i could pass a function to a function
processMyData(doMoreStuff);

Open in new window


Without callbacks I could do
function doMoreStuff(myString) {
  alert(myString)
}
function processMyData(func) {
//do something
   
return true;
}



if (processMyData()===true)
{
doMoreStuff('success');
//do other stuff, call other functions

}
else
{
alert('fail');
}

Open in new window

Avatar of jaylab2

ASKER

Thank you for your reply but can you give example on my script so it will help understand more.
ASKER CERTIFIED SOLUTION
Avatar of OriNetworks
OriNetworks

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial