jqueryyiijquery-trigger

Yii, jQuery: can I make jQuery code generated by ajaxLink() load before other scripts?


I'm making tabbed content in Yii manually, so the content would load data from AJAX. I'm using ajaxLink() and it works fine. The selected tab ID is stored in session so when I reload the page, the same tab is selected again. The problem is, the content will load only when I press a tab(link), instead of loading automatically. So I figured out I could use jQuery.trigger() function to simulate a click on page load. But it doesn't work, probably because Yii generates the script and places it at the end of the page, so the .trigger("click") function doesn't do anything - it can't access the AJAX code because it isn't generated yet.

Is there a way to solve this problem? I know this is a bad solution but I have to do it that way.


Solution

  • You shold add script which triggered event after the DOM is ready:

    echo CHtml::ajaxLink(
         'linkText', 
         'url', 
         array('update'=>'#conteinerId'),
         array('class'=>'activeTabLink')
    );
    
    Yii::app()->clientScript->registerScript( 
       'ajaxTabLinkScript', 
       '$(function(){ $(".activeTab").click()})', 
       CClientScript::POS_LOAD 
    );
    

    Pay attention to CClientScript::POS_LOAD parameter. Thanks to him, this function is performed after to link is added the update event.