Html::a With Option Target => _Blank

Hi everybody!

How to add option target=>_blank in Html::a() ?

I have handled this as following :

Html::a(name, url, [‘target’=>’_blank’]), but it don’t effect.

Anybody know this ? Thanks

Try ‘linkOptions’ => [‘target’ => ‘_blank’]

I’m not sure that it is relevant here, but I use such approach in NavBar:

$menuItems[] = ['label' => Icon::show('question-sign', [], Icon::WHHG).'Help', 'url' => ['/site/about'], 'linkOptions' => ['target' => '_blank']];

Hi Fortis!

Great! You’re correct, thank you for your good reply.

With Navbar, i also test it is ok,but i can’t do this for Html::a().

This should work - not sure what problem you are facing:

echo Html::a('Link', ['/site/test'], ['target'=>'_blank']);

Well… It seems, that someone is facing similar problem:提一个有关yii2的html类的bug/

I think, you could try using Html::tag("a" … ): ; In the official docs there is an example with target="_blank", so, if this is not working too, then probably it is a bug…

Technically the Html::a should render the target=’_blank’ markup without issues. The issue in the example link mentioned above is that Html::a is used within a grid view column. The GridView data column uses the Yii Formatter to render the column. If the format is set to raw, it will work in the above case (there is no formatting applied to the markup). The example probably uses the html format for the column in the grid view, which runs through Html Purifier which may be stripping these markups.

1 Like


Great! Kartik V is correct.

I test very well with ‘format’ => ‘raw’.

Thank verybody.

1 Like

The problem may also be in pjax. Even if you use the row format. In this case just add "linkSelector" param:


    ['id' => 'samle', 'linkSelector' => 'a:not(.linksWithTarget)']


and add corresponding css class to your links:

return Html::a(


    ['/view', 'id' => $model->id],

    ['target'=>'_blank', 'class' => 'linksWithTarget']


This will prevent only these links from pjax so they can be open in new tab.

Hope it will help someone.

pjax was the issue. Thank you very much for your help

Thanks a lot; this worked for me to show the link from inside a GridView widget.

<?php Pjax::begin(['linkSelector' => 'a:not(.linksWithTarget)']); ?>

<?= GridView::widget([

        'dataProvider' => $dataProvider,

        'filterModel' => $searchModel,

        'columns' => [

            ['class' => 'yii\grid\ActionColumn'],





              'attribute' => 'additional_file',

              'format' => 'raw',

              'value'=> function($model) {

                  if(!empty($model->additional_file)) {

                    return Html::a('Show additional file',


                      ['target' => '_blank', 'class' => 'linksWithTarget']






    ]); ?>

<?php Pjax::end(); ?>

Don’t forget about