Hi Umar,
Maybe your script runs too early? I found it quite hard to detect when a mendix page is fully loaded to determine when a java script widget should fire. Try setting a timeout
setTimeout(function(){ }, 3000);
around that java script. the document.ready function did not work reliably for my case when I needed it..
Edit: of course setting a timeout is not reliable either as it still depends on your page loading, but it is nice to check and maybe find the culprit