Topic: How javascript can "read" html code generated by xajax asynchronously

hi, I need to apply javascript code to xajax response

Is there any way to do that?

Because in a javascript code I say (for example) "slmenuObj = document.getElementById('slidedown_menu');"
and then "var mainUl = slmenuObj.getElementsByTagName('UL')[0];"
but  slmenuObj.getElementsByTagName('UL')[0] is empty, because the html code was written asynchronously by xajax
and if I check the html source the 'slidedown_menu' is empty.

Thanks..

Re: How javascript can "read" html code generated by xajax asynchronously

I'd founded the solution, I put "$objResponse->script("myJavascriptFc.js");" after "$objResponse->assign('slidedown_menu', 'innerHTML', $content);"
And load the js file in the main head tag of the html.

Re: How javascript can "read" html code generated by xajax asynchronously

Sorry,
Sorry, I also have the same problem, but you solved it, I turned my will that can help me, please
These variables are necessary, but must be within the function should not be in body of HTML
var todaydate=new Date();
var curmonth=todaydate.getMonth()+1; //get current month (1-12)
var curyear=todaydate.getFullYear();
My code is:

<?php
  require ('xajax/xajax_core/xajax.inc.php');
  $xajax = new xajax();
  $xajax->configure("javascript URI", "xajax/");
  $xajax->setCharEncoding('ISO-8859-1');


  $xajax->registerFunction("showCalendarJS");
  function showCalendarJS()
  {
    $objResponse = new xajaxResponse(); 
    $objResponse->includeScript("js/basiccalendar.js");
    
    $csetup = "document.write(buildCal(curmonth-1 ,curyear, 'main', 'month', 'daysofweek', 'days', 1))";
    $objResponse->script("var todaydate=new Date()
var curmonth=todaydate.getMonth()+1 //get current month (1-12)
var curyear=todaydate.getFullYear()");
    $objResponse->script("setTimeout(\"$csetup\",10)");
    $objResponse->assign("slider2","innerHTML",$csetup);

    return $objResponse;
   }

  $xajax->processRequest();

?>

<html>
<head>
<?php $xajax->printJavascript(); ?>
<style type="text/css">@import url(js/calendarsimple.css);</style>
</head>

<body >
<div id="slider2"></div>

    <script language="javascript" type="text/javascript">
        xajax_showCalendarJS();
    </script>
</body>

</html>

Re: How javascript can "read" html code generated by xajax asynchronously

There are a couple ways to solve this... a couple different ways of looking at it.

1)  Make a javascript function to perform the javascript work (assuming you don't need to pass too many values to the function to get the job done).  By building the javascript once and including it in a .js file, you simplify your php code and reduce the amount of dynamic content being sent to the browser after the page is loaded.  You can always call your javascript function (that is loaded via a .js file) via a xajax response command $objResponse->call('methodName' [, $parameterOne, $parameterTwo, etc...]);

2)  Make a javascript function dynamically using the xajax ->setFunction call:
$objResponse->setFunction('functionName', 'arg1, arg2, arg3', ' var someJavascript = arg1; var more = arg2; var el = document.createElement(arg3); el.innerHTML = more;');
Then call the function from your request handler:
$objResponse->call('functionName', $valueOne, $valueTwo, $valueThree);

It depends on what you are trying to accomplish.  If you need to dynamically send javascript functions to the browser during the lifecycle of the page, then by all means, use the second method to build your custom javascript and send it to the browser as a function, then call the function.  In this fashion, you can also build javascript event handlers and dynamically change their content.  It is a very powerful feature.

Just keep in mind, it is not always necessary to send each javascript command to the browser via a ->script call.  Sometimes, it is better to formulate a javascript method, send the method to the browser, then call it.  If you have a large amount of javascript and you don't mind sending it via a .js file (which makes it easier to "read" by a person snooping around your page code), then by all means, pre-build it into the .js file and deliver it all at once (either at IPL or via a ->includeScript response command.

// Joe

xajax Developer
Connect to me on LinkedIn:
http://www.linkedin.com/in/calledtoconstruct

5 (edited by luismarcelo 2009-07-10 2:40:32 AM)

Re: How javascript can "read" html code generated by xajax asynchronously

Thanks for your explanation CTC, try to call the function but not me, but I came across a possible solution, but your explanation made me realize that my solution will only be temporary, and would like you could give me a ejemplito please, look this is my possible solution.

<?php
  require ('xajax/xajax_core/xajax.inc.php');
  $xajax = new xajax();
  $xajax->configure("javascript URI", "xajax/");
  $xajax->setCharEncoding('ISO-8859-1');


  $xajax->registerFunction("showCalendarJS");
  function showCalendarJS()
  {
    $objResponse = new xajaxResponse(); 
    $objResponse->includeScript("js/basiccalendar.js");

//These belonged to javascript variables were created, and would like to know how they are handled.
    $curmonth=date('n');
    $curyear=date('Y');

    $csetup = "document.write(buildCal(".$curmonth." ,".$curyear.", 'main', 'month', 'daysofweek', 'days', 1))";
    $objResponse->script("setTimeout(\"$csetup\",10);");
    $objResponse->assign("slider2","innerHTML",$csetup);

    return $objResponse;
   }
  $xajax->processRequest();

?>

<html>
<head>
<?php $xajax->printJavascript(); ?>
<style type="text/css">@import url(js/calendarsimple.css);</style>
</head>

<body >
<div id="slider2"></div>

    <script language="javascript" type="text/javascript">
        xajax_showCalendarJS();
    </script>
</body>

</html>

These belonged to javascript variables were created, and would like to know how they are handled in XAJAX because variables are created in a <script>, with an example please.

Re: How javascript can "read" html code generated by xajax asynchronously

Hello CTC,
I know it is an older Post,
You Wrote:
Make a javascript function dynamically using the xajax ->setFunction call:
$objResponse->setFunction('functionName', 'arg1, arg2, arg3', ' var someJavascript = arg1; var more = arg2; var el = document.createElement(arg3); el.innerHTML = more;');

Wenn i do this with an simple .js function, it will not shown in the Browser
Do you have an idea?

Re: How javascript can "read" html code generated by xajax asynchronously

...okay, i had solved the Problem.

I had tried to register an new xajax function via response.
It will function