Topic: Unobtrusive $xajax->printJavascript();

Instead of having xajax print the constructed javascript inside the head tag, why not create a php file that can be included as a script.

ie. <script type="text/javascript" src="xajax/constructed_javascript.php"></script>

xajax/constructed_javascript.php would contain the the equivalent to $headJS = $xajax->getJavascript('xajax/');

Re: Unobtrusive $xajax->printJavascript();

I believe this has been discussed before... however, I'm sure the outcome.

There are some problems with doing so... mainly in that the browser may choose to cache this file... which is both a benefit and a potential pitfall.  During development, you would not be able to update this file reliably if the browser was caching it.  During production, however, it would be nice if it were cached.

It should definitely be considered, if nothing else, as an option that could be configured.

Thank you for bringing the subject back to the forefront. smile

// Joe

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

Re: Unobtrusive $xajax->printJavascript();

I can see where caching would be a problem, but I am sure many others have a similar setup as I do where there is a main xajax config that is rarely untouched between dev and live sites. This would be optimal for a setFlag or other configuration option as you mentioned.

Re: Unobtrusive $xajax->printJavascript();

Yes.  We've been discussing this in the xajax IRC channel.  I think it will be workable.  I'll test it and see how it works / how it effects performance.

// Joe

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

Re: Unobtrusive $xajax->printJavascript();

Thanks! I've been thinking about this for a while, and finally got bugged enough from xajax being the only printed javascript on my pages to say something. It would be great to see this as a new feature!

Re: Unobtrusive $xajax->printJavascript();

Ok, I've actually implemented a test version for this.  I added a configuration option:

$xajax->configure('deferScriptGeneration', true);

That will cause the bulk of the javascript codes to be deferred.  Then a <script> tag is inserted with a SRC attribute referring back to the script (using the requestURI) with a hash code for uniqueness (will help elliminate problems with caching when the script changes).

I tested using firebug and found that the new method (deferred script) is slightly slower as it generates another round trip to the server... however, this is offset by a faster initial load of the page in many cases.  Also, if the browser caches the script callback (since the hash code will only change if the script changes), it will further provide benefits upon repeated visits to the page.

Please take a look at the beta version from SVN and let me know what you think.

// Joe

p.s.  It is also possible to remove the xajax_core.js load validation script by setting the delay time to zero.  This would leave only the <script> tag for the xajax_core.js and the <script> tag for the callback to the request URI.

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

Re: Unobtrusive $xajax->printJavascript();

This is what I got when I uploaded the new version.

<b>Notice</b>:  Undefined offset:  1 in <b>/home/blueAlien/public_html/includes/xajax/xajax_core/plugin_layer/xajaxScriptPlugin.inc.php</b> on line <b>104</b><br />

<br />
<b>Notice</b>:  Undefined offset:  1 in <b>/home/blueAlien/public_html/includes/xajax/xajax_core/plugin_layer/xajaxScriptPlugin.inc.php</b> on line <b>105</b><br />

8 (edited by CtC 2007-07-19 12:31:58 AM)

Re: Unobtrusive $xajax->printJavascript();

Thanks for the report, I'll check into it and see if I can reproduce the error.

EDIT: Ok, I checked the routine... and I checked in some changes that should handle the situation.  Basically, the script that was being generated was not likely enclosed in <script> and </script> tags... so the routine didn't know what to do with it.  Did your script register any functions?

Anway, get latest from SVN when you get a chance and let me know how it goes.  Also, when updating, please remove (or rename) the previous xajax folder before copying in the new files.  (just to be sure) smile

// Joe

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

Re: Unobtrusive $xajax->printJavascript();

The external javascript has xml in it.

<xjx>
<cmd n="dbg">Invalid script or style request.</cmd>
</xjx>

Re: Unobtrusive $xajax->printJavascript();

Also I just tried setting the timeout to zero using

$xajax->setTimeout(0);

This did not work however and I received this error

Call to undefined method xajax::setTimeout()

I checked xajax_core/xajax.inc.php, and could not find the function in there.

Re: Unobtrusive $xajax->printJavascript();

Sorry for the lack of details... time is a premium these days. sad

$xajax->configure('scriptLoadTimeout', 0);

Give that a try and see if xajax leaves the validation routine off (the one that checks to see if the xajax javascript file is loaded).  I don't necessarily recommend this being set to off as you would no longer know if your javascript file failed to load, except that the xajax functions would fail.

I'll continue to work on that feature and try to clean some things up.  I'm not 100% sure if it will be supported or not, but I hope to provide it as an option.

// joe

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

Re: Unobtrusive $xajax->printJavascript();

The external javascript is still not displaying any javascript for the script deferral

Re: Unobtrusive $xajax->printJavascript();

Ok, finally, I figured this one out... you'll need to specify the requestURI:

$xajax->configure('requestURI', 'yourfile.php');

Otherwise, xajax will detect it and it will include the ?xjxGenerateScript=hash_code

Try it out and let me know how it goes. big_smile  (I tested this with my scripts and they worked great)

// Joe

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

Re: Unobtrusive $xajax->printJavascript();

Maybe I am doing something wrong? Would you take a look at these source files?
http://www.blue-inc.us/~blueAlien/inclu … x.inc.phps
http://www.blue-inc.us/~blueAlien/includes/head.phps
http://www.blue-inc.us/~blueAlien/hmm.phps

The working page is http://www.blue-inc.us/~blueAlien/hmm.php

Re: Unobtrusive $xajax->printJavascript();

I don't see any issues with the scripts given above.  neutral

I have checked in some minor changes, when you have the chance, get the latest and let me know if there is any change.  If the error still occurs, try uncommenting the three echo statements in the xajaxScriptPlugin and let me know what comments get printed to the output.

Basically, here is what is going on:  When the <script> tag reference is issued, a hash is generated based on the content of the script that would have been sent to the browser.  This has is compared to the script that would be sent to the browser during the deferred script generation.  For some reason, the two hashes do not match.  So... for some reason, the same code is producing different output based on whether it is a request for the initial page load and a request for the deferred javascript.

// Joe

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

Re: Unobtrusive $xajax->printJavascript();

The first commented line put this right before the js include file

<!--Array
(
    [0] => 
/* <![CDATA[ */
try { if (undefined == xajax.config) xajax.config = {}; } catch (e) { xajax = {}; xajax.config = {}; };
xajax.config.requestURI = 'hmm.php';
xajax.config.statusMessages = false;
xajax.config.waitCursor = true;
xajax.config.version = 'unknown';
xajax.config.legacy = false;
xajax.config.defaultMode = 'asynchronous';
xajax.config.defaultMethod = 'post';
/* ]]> */

    [1] => 
/* <![CDATA[ */
xajax_Button = function() { return xajax.request( { xjxfun: 'Button' }, { parameters: arguments } ); };
xajax_showDatabase = function() { return xajax.request( { xjxfun: 'showDatabase' }, { parameters: arguments } ); };
xajax_writeConfigure = function() { return xajax.request( { xjxfun: 'writeConfigure' }, { parameters: arguments } ); };
xajax_test = function() { return xajax.request( { xjxfun: 'test' }, { parameters: arguments } ); };
xajax_addWindow = function() { return xajax.request( { xjxfun: 'addWindow' }, { parameters: arguments } ); };
xajax_hmm = function() { return xajax.request( { xjxfun: 'hmm' }, { parameters: arguments } ); };
xajax_hmm2 = function() { return xajax.request( { xjxfun: 'hmm2' }, { parameters: arguments } ); };
/* ]]> */

)
-->

The second commented line didn't add anything.
The thrid comment line added this to the included javascript file.

<!--Array
(
    [0] => 
/* <![CDATA[ */
try { if (undefined == xajax.config) xajax.config = {}; } catch (e) { xajax = {}; xajax.config = {}; };
xajax.config.requestURI = 'hmm.php';
xajax.config.statusMessages = false;
xajax.config.waitCursor = true;
xajax.config.version = 'unknown';
xajax.config.legacy = false;
xajax.config.defaultMode = 'asynchronous';
xajax.config.defaultMethod = 'post';
/* ]]> */

)
--><?xml version="1.0" ?><xjx><cmd n="dbg">Invalid script or style request.</cmd></xjx>

Re: Unobtrusive $xajax->printJavascript();

Ok, basically that is what I expected to see (a discrepency between the first and third).

For some reason, the xajaxFunctionPlugin is not generating the function stubs during the deferred script generation process.  I believe this may have to do with the order the plugins are loaded (although it *shouldn't*)  This would explain why I can't reproduce the issue on my two servers.

neutral

I suppose I'll need to force xajax to load the plugins in alpha(numeric) order and then name the plugins appropriately.  It is an extra step, but not a big deal considering the benefit.

Thanks for your patience.

// Joe

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

Re: Unobtrusive $xajax->printJavascript();

Take your time, I am in no hurry for this project. Thanks for all the great help you given!

Re: Unobtrusive $xajax->printJavascript();

Ok, I've checked in some changes that allow plugins to specify a number relating to the order the plugins will be accessed... this "should" take care of the issue you are experiencing.

However, I'm not 100% sure that PHP (all versions at least) return array elements in the order of the key values... so it is possible that this scheme will not ensure the order of the plugins.

Does anyone have experience with ordering items in a php array by setting it's index value explicitly?  More specifically, would foreach (array_keys($aArray) as $sKey) always return 1, 2, 3, 4, 5 even when the items are added like = array( 5 => 'abc', 2 => 'def', etc...) ??

Thanks in advance.  blueAlien, give that a try and let me know; (if necessary, you can uncomment those same echo statements for debugging)

// Joe

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

20 (edited by blueAlien 2007-07-30 6:36:56 AM)

Re: Unobtrusive $xajax->printJavascript();

http://www.php.net/manual/en/function.ksort.php
http://www.php.net/manual/en/function.krsort.php

This may help


Although I am not even getting the functions part of the array on the javascript file, just the config part of the array

hashes still don't match

Re: Unobtrusive $xajax->printJavascript();

blueAlien,

I have checked in some additional updates for the script deferral feature.  If / when you have some time, please let me know if the latest updates do the trick. smile  Thanks for the references to the ksort function; I think php requires a manual sort of the array, so it is likely that this was the problem, despite having the ordered index value.

// Joe

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

Re: Unobtrusive $xajax->printJavascript();

Hi!

The deferral option is working for me, thanks!

JS

Re: Unobtrusive $xajax->printJavascript();

Is this a feature plug-in?  If so where is the link?  thanks for your help.

Re: Unobtrusive $xajax->printJavascript();

Hey All,

This works really well!

I have created the following include file that will become part of my "standard":

if (isset($uriName))
{
    $xajax->configure('requestURI', $uriName);
    $xajax->configure('scriptLoadTimeout', 0);
    $xajax->configure('deferScriptGeneration', true);
}

It depends on the variable "uriName" which is defined as

$uriName = basename(__FILE__) in the parent script.

Now I can leave the variable undefined until the script goes to production.

Ed

If you ever stop learning you may as well dig a hole, crawl in and pull the top over yourself.

Re: Unobtrusive $xajax->printJavascript();

Thanks ed,  I have finished my version of this.  Updated.  Here you go for the others.

http://community.xajaxproject.org/viewt … 628#p26628