how to add anchor to url with render()

With redirect() it is possible to add an anchor to the url.

$this->redirect(array('controller/action', '#'=>'anchor'));

Is there also a solution when using render() instead of redirect()?

the render function renders a page while the redirect function redirects to url given an array to construct a url, I don’t get what you mean?

you mean you want to create a link that links to a url with an anchor?

Sorry that my question isn’t formulated more clearly. I would like to render a view (which contains a form) and jump to a particular anchor in that view. The view contains multiple anchors and by supplying an anchor in the url the browser jumps to that anchor. I can not use a redirect as the rendering is part of a form handling.

Any ideas, anyone?

There’s a logical problem in your thoughts: When the browser requests a page, it sends the URL like:

Now a Yii action is called and a view gets rendered. You then want to change the URL to something like:



How should that work? You can’t change the requested URL when rendering a view. All i can think of is to add a little piece of javascript that executes on DOM ready and adds a anchor to the window.location.

This can be a workaround:

First, add a pseudo-hash ID when rendering a page in your controller:

$this->render('your view',array('model'=>$model,'hash'=>'your ID here'));

Second, in ‘your view’, add a jquery onload function:

[html]<input value="<?=$hash?>" id="hash" type="hidden" />

<script type="text/javascript">



	location.hash = &quot;#&quot;+&#036;('#hash').val();		



Third, add the anchor to your desired location on the page:

[html]<span id="your ID here"></span>[/html]

You’ll notice that in your url, the hash tag is now included.

Hope this workaround helps.