NLSClientScript prevents duplicated linking of js and css files when updating a view by ajax, when eg. paging or sorting a gridview, ajax-submitting a form or any custom ajax-updating a part of a view.
The issue what this extenson fights is for example when you render Jui widgets by CHtml::ajax, the js files used by the widget will be loaded as many times as you render such a widget in a view. The unnecessary bandwidth usage is the smaller problem, the bigger problem is eg. loading jquery.js again may reset some js objects set by previously loaded ui-related js files. That can cause js errors and the view may stop working.
Using NLSClientScript helps to avoid it all.
5.0 (see the updated Usage)
4.0 RC
3.6
3.5 (see the updated Usage)
3.4
3.3
3.2
3.1
3.0
2.1
2.0
1.3
1.2
If you interest the details, see the comments in the source.
Yii 1.x
1 . Simply set the class for the clientScript component in /protected/config/main.php, like
... 'components'=>array( ... 'clientScript' => array( 'class' => 'your.path.to.NLSClientScript', 'excludePattern' => '/\.tpl/i', //js regexp, files with matching paths won't be filtered is set to other than 'null' //'includePattern' => '/\.php/' //js regexp, only files with matching paths will be filtered if set to other than 'null' ) ... )
Warning: in 5.0 the two optional parameters have been renamed!
2 . use Yii::app()->getClientScript() by the standard way to link js and css files/snippets
Example:
$cs = Yii::app()->getClientScript(); $systemJsPath = Yii::app()->getAssetManager()->publish( Yii::getPathOfAlias( 'system.web.js' ), false, -1, false ); $cs->registerScriptFile( $systemJsPath . '/ext/yii_ext.js'); $cs->registerScriptFile( $systemJsPath . '/ext/plugins/jquery.form.js');
3 . If you want to do a custom ajax request with "dataType"="json" and there are some fields of the response you want to update your page with, filter that part "by hand" with $.ajaxSettings.dataFilter like eg.:
echo CHtml::ajaxLink('custom update', array('/site/testupdate'), array( 'dataType' => 'json', 'success'=>'js:function(data){ $("#cont").html($.ajaxSettings.dataFilter(data.content)); }', ));
Total 20 comments
Hi
I just tried version 5.0 after previously using v3.6.
I use it as in the example: $("#cont").html($.ajaxSettings.dataFilter(data.content)); . Apparently this does not work anymore ad 'dataFilter' is unknown in Chrome (for example) when using 5.0. It appears that 'dataFilter' is defined only when using 'IE'.
For those that use NLSClientScript to create jQuery interface elements: I upgraded to 5.0 to see if this would fix another issue I have. When doing $("#cont").html(...) to create a jQuery dialog, I now conclude the 'div' is moved elsewhere by jQuery.dialog which is responsible in my case of creating multiple dialogs that eventually conflict with each other... That's not a NLSClientScript issue in the end ;-) and I'll have to fix this differently.
Using version 5 seems to work as expected in IE7/8 and Chrome. I upgraded after IE7/8-issues in version 3.3.
Only thing I would like to see is proper DocBlock commenting on variables and functions in your extension.
And actually, maybe a better name because "NLSClientScript" doesn't say anthing about what it does, but something like "AjaxCompatibleClientScript" or maybe just "EClientScript" or "XClientScript" is better.
You are right. My 'static' pages register a small js script. But how you said they don't need jquery.
I register now jquery for all my sites in my main layout view.
I use the google lib and I think it is no problem for performance.
I guess that is not a static page where the issue occurs. On the pages where no script file registered, the component renders nothing. On your "static page" some script is registered requires no jquery, that's when the issue occurs.
So yes, for now all pages need jquery registered since the component uses it. I'll fix in the next release to register jquery automatically in every case.
I have install version 5.0.
It works on all my pages where jquery is included.
But on my static pages (e.g. impressum page) where I need no jquery there I get the error 'jquery undefined'.
What can I do. Must I include on all my pages jquery or can I do this with exclude pattern ?
test results about usages of the latest version are welcome
IE7 has a problem with the "src" attribute of the script tag, as can be seen in http://bugs.jquery.com/ticket/11404
So the update of the grid returns the alert "Parser error".
The solution to the problem would be to change the script to:
is #7098 fixed in 3.6 version? I am still facing the problem in filtering in the cgridview
I have problems. When the extension is enabled the HTML content is being duplicated in ajax requests.
Detail: the problem does not occur in IE, only in Firefox and Chrome. And only occurs when the return of Ajax is the complete page, including the HTML tag.
The javascript code behaves completely different in IE vs Firefox and Chrome.
The developer needs to fix the default behavior on all browsers.
A workaround for Firefox and Chrome (but certainly not the ultimate solution):
hoplayann thanks for the report, i'll check and will release a new version in a few days, there's also an other issue needs to be fixed.
Hello,
I am using version 3.6 of your extension in my Yii project.
It seems that the selection of script tags in the HTML source fails for some reason in Internet Explorer 7. The looping through the SCRIPT or LINK elements is the place where we have now added an extra test case, to be sure that there is at least a not empty SRC or HREF attribute defined. (see added code below)
And I had to add the code again in the other loop :
Is it possible that the jquery selector is not precise enough ?
Please let me know if there would be a fix for that issue in your next revision.
Yann
fixed in 3.6
Here is returning an error warning that "head" is not defined.
This piece of code:
should be?
yep, 3.5 works like expected. thanks for your work and a happy new year!
check 3.5 and also the demo on nlacsoft.net (it does exactly your case:))
Hi,
unfortunately this new version does not fix the problem. I'm encountering this problem in chrome 15 on kubuntu and in other browsers. I've got a lot of work to do at the moment because it is the last workday for this year (yay), but i'm going to create a testcase for this problem in the new year.
Hi, i tested the bug, actually i experienced such issue only in IE (not exactly the same issue but can be related) and added a fix in 3.4. i'm curious if it has been fixed at your case
Thanks, excellent catch and solution. I'll include it in the next release, also have to fight other cases (dt,dd,li,caption,thead etc)
Hi, thanks.
"NLS" points to "NLacSoft" what is my domain name actually. (i'll release some valuable content there in a few days!:))
The "EClientScript" is a kinda standard extension name format, i use my extension with that name at the company i work for.
hi, i've switched from 2.1 to 3.3 today and found the following bug:
in a form which is organized in a table, the user can add new rows via ajax. the server is sending only a new table row (tr-element) with some columns in it.
the javascript-filter takes this row and puts it via innerHTML into a div-element which kills all tr- and td-elements from the original code. it then filters the dom and returns the broken code.
quick-fix:
insert above line 74:
Leave a comment
Please login to leave your comment.