3

This is my code snippet:

function MobileDealLink(pURL) {
    if (document.documentElement.clientWidth < 501 && pURL != '0') {
        window.location=pURL;
    }
}

What happens is,if a user presses a div that is calling this function, that particular user is successfully redirected to that link. But when he presses the back button on his device, he is sent to the page prior to visiting the page that ran the function.

Here's the actual scenario:

  1. User googles for property; enters website through Google results page
  2. Sees property listings on homepage of website, and presses a specific property listing div
  3. Div onclick runs function, sends user to full listing page of specific property
  4. User now presses the back button on his device (wishing to go back to the website homepage)
  5. Device sends user back to Google and not back to homepage (This is the baffling issue I'm facing.)

How do I stop this happening? I believe this is greatly affecting the bounce rate of the website by throwing the visitor straight back to Google instead of going back to the homepage and assisting in a "textbook style sales funnel" experience.

*You can witness this for yourself on the website: https://www.propertypost.lk

Chamila Maddumage
  • 3,304
  • 2
  • 32
  • 43
Ihsan
  • 140
  • 1
  • 1
  • 12
  • 1
    this looks similar to what you're asking. I haven't verified this will work myself. https://stackoverflow.com/questions/53112946/cant-go-back-after-changing-window-location-href – Our_Benefactors Jan 22 '19 at 04:58
  • Thank you @Our_Benefactors, and all the other folk. I'm afraid the issue is quite different. There's been some other JS that's been overriding the function. This bit of code: window.location.replace(). So when the user clicks, the page is being replaced and not redirected... *facepalm. – Ihsan Jan 22 '19 at 08:02

2 Answers2

1

Try using window.location.href = pURL hope it will work for you, tested its running fine for me

mzparacha
  • 627
  • 6
  • 20
0

I would recommend you to go through this stack

Call a JavaScript Function when Back Button of Browser is Clicked

Try using unload() for jquery or onbeforeload() for javascript and redirect to the search result page. This redirection url can be stored in a local storage if needed.

Aravind Anil
  • 153
  • 1
  • 9
  • 1
    Thank you Aravind, but no, I don't want extra code on whatever the target page is, to store the URL, and call a function to redirect to a specific URL. That would be too much of work and could cause scalability issues in terms of a simple link press. (In fact, I would have simply used the html anchor tag instead of javascript, but I'm using JS so to only allow the link to work on smaller screens.) – Ihsan Jan 22 '19 at 05:49