phpjavascriptyiizii-widgets

Yii: Open url in new Window, not in new tab


Has anyone tryed to set a CMenu link to open in a new window?

Mine, opens the new window, as blank page and still goes to the requested url

<?php
$this->widget('zii.widgets.CMenu', array(
    'items' => array(
        array('label' => Yii::t('admin', 'Live Reports'), 'url' => array('/admin/liveReports/index'), 'visible' => !Yii::app()->user->isGuest, 'active' => ($this->id == 'liveReports'), 'linkOptions' => array('onclick' => 'javascript:window.open("/admin/liveReports/index","x","width=200,height=100")')),
    ),
));
?>

Solution

  • You forgot to return false; from the onclick attribute, which is why the current window/tab still navigates to the url:

    'linkOptions' => array(
        'onclick' => 'javascript:window.open("/admin/liveReports/index","x","width=200,height=100"); return false;'
    )
    

    Consider using an onclick event handler instead, for good practice, i.e Unobtrusive Javascript.