How to append to URL from controller

pretty simple request. i have a search box on the front-end page that submits to the controller. the controller does the search and returns the results. all is good and working.

however, i’d like to be able to append the string “#searchId” to the URL so the search page scrolls to the ID selector of the html element.

can i append from the controller? or do i have to do some logic in the routes?

when i click the search button, it appends the query to the URL:

image

i just wan to be able to append so the URL looks like: localhost:8080/xxx/resources?q=hello#searchID

controller code looks like this:

return function ( $page ) {
$query = get(‘q’);
$items = $page->children()->listed();

    if( $query != null && !empty( $query )){
        // search by title and tags for individual resource
        $items = $items->search( $query, 'title|previewtitle|tags' );
    }

    $results = $items->paginate( 10 );

    return [
        'query' => $query,
        'childpages' => $results,
        'pagination' => $results->pagination()
    ];
};

consider adding a js script only when you have search results to make the browser scroll once the dom is loaded. the script has to be after the targeted id.

<div id=“searchID”>my search results</div>

<script>document.getElementById("searchID").scrollIntoView({block:"nearest", behavior:"smooth"});</script>