Expand/collapse based on page redirect

Guys, I have a situation where something that was working previously, doesn’t appear to be now - at least not all of the time.

I have a page on my website called Locomotive Logs which can be accessed via a button on the home page leading to a menu page where user can navigate to the page of the choice using dropdown menus.

I also have a search function on my site which enables the user to draw up results to a search which then offers a link to the above mentioned Locomotive Logs pages. So for example, a user searches for locomotive 68001 and when they get the results for this, they are offered a button which when clicked will redirect them to the Locomotive Log page for 68001. However, if they access it via the button in the search results, I require the page layout of the Locomotive Log page to differ slightly to when they access it via the button on the home page (with some elements collapsing and others expanding).

The relevant piece of code for this is below:

Search Result Page:

export function button12_click_1(event) {
     

 let $item =  $w.at(event.context);
 let currentItem = $item('#Shunters').getCurrentItem();
 let dynamicPageID = `${currentItem.number}`
 let collectionID = `${currentItem.title}`
 
   console.log('https://www.ukraillog.co.uk/'+collectionID+'/'+dynamicPageID);
 wixLocation.to('https://www.ukraillog.co.uk/'+collectionID+'/'+dynamicPageID+"?from=locomotive-results");
 }

Locomotive Logs page:

export function button12_click_1 (event, $w){
let path = wixLocation.path;
console.log(path);
if (wixLocation.path[0] === "locomotive-results"){
    
$w('#button1').hide();
    console.log("Back button is hidden");
}
}

let query = wixLocation.query;

if (query.from != undefined){
    console.log('Query From = '+ query.from);
    $w('#classDropDown').collapse();
    $w('#numberDropDown').collapse();
    $w('#button1').collapse();
    button15_click_1();
    $w('#button14').hide();
    $w('#button16').show();
    $w('#text541').show();
}else{
    $w('#classDropDown').expand();
    $w('#numberDropDown').expand();
    $w('#button14').show();
    $w('#button1').expand();
    $w('#button16').hide();
    $w('#text541').hide();
}

Now, I don’t know when this issue began as this area of the site is one where I am slowly adding data to and due to other things taking over, I’d not done much with it for a while. I have added data today and when I was testing that this data had gone in ok, I noticed the issue I am now having which is detailed below and only relates to when the user is clicking the button in the search results page:

Currently I am experiencing a situation whereby sometimes everything works as it should and when I redirect from the search result page, the layout of the Locomotive Log page is as it should be. But then on other occasions, I click to redirect and I am met with the page layout I would expect if I had accessed the Locomotive Logs page from the Home Page menu buttons - in essence, the elements that are meant to collapse don’t and neither do those that are supposed to expand.
Whenever the relevant button is clicked, the redirect works fine and takes you to the correct Locomotive Log page which would suggest the issue lies in the Locomotive Logs page.

What I am finding strange is why it seems to work sometimes but then not on other occasions and so I am hoping that somebody out there understands the above drivel and is able to point me in the right direction.

Thanks