www.webdeveloper.com
Results 1 to 8 of 8

Thread: [RESOLVED] Back Button with two function

  1. #1
    Join Date
    Dec 2008
    Posts
    138

    resolved [RESOLVED] Back Button with two function

    Is it possible to have a back button feature that goes back from the previous page if it was coming from the website itself (i.e. page 1 to page 2). But when user come from other site (i.e. google search result) it links to a general page from my website. It can be javascript or php. Can someone pls give me an example script or link to a site that can get me started? thx.

  2. #2
    Join Date
    Mar 2012
    Posts
    1,748
    Preventing the back button from exiting your site is one sure way of losing traffic!!!! Don't you get pissed off when other sites do it to you???? Do you return to such sites??? Why do you think anyone would touch your site with a disinfected barge-pole if you do things like that???

    Rant over. But seriously: If your site has a high bounce rate, that should be telling you something. Most likely:
    - Your site either has a misleading URL, keywords or description (so people are visiting it who are not interested in it), or...
    - It is crap!!!!

    Either way, fix the problem not the symptom!!!

  3. #3
    Join Date
    Dec 2008
    Posts
    138
    jedaisoul, I should clarify what I am trying to do. I am not trying to change the back button on the browser. I've created a button named "back" with href="javascript:history.go(-1)" and it was meant to have user go back to the previous page from my site that linked to this page. However, if they landed on this page from other site (i.e. google) I want them to go to a general page instead. If they want to back to google they can use the browser's button. Nothing to do with bounce rate as it is not even up yet.
    Last edited by phpnewbie08; 04-13-2014 at 03:14 PM.

  4. #4
    Join Date
    Mar 2012
    Posts
    1,748
    Thanks for clarifying, my apologies for misinterpreting your post.

  5. #5
    Join Date
    Mar 2005
    Location
    Behind you...
    Posts
    1,016
    You can check the 'document.referrer' in javascript to get the previous page, from which you can then check to see if this page was on your site or an external URL such as google.

    HTML Code:
    <a href="javascript:void(0);" onclick="_GoBack()">Back</a>
    
    <script>
      function _GoBack() {
        if(document.referrer.toLowerCase().indexOf("your-site.com") >= 0) {
          history.go(-1);
        } else {
          window.location.href = "http://www.your-site.com/general/page.html";
        }
      }
    </script>

  6. #6
    Join Date
    Dec 2008
    Posts
    138
    Works great thx Sup3rkirby.

  7. #7
    Join Date
    Dec 2008
    Posts
    138
    There is a problem with this script. Not sure how to explain this but here it goes. If I go back then click on 2nd link from page 1 then go back click on 3rd page from page 1 then go back the script seems to give me the wrong history (i.e. showing the page on link 3 instead of page 1) after a few times. Is there a fix for this?

  8. #8
    Join Date
    Mar 2005
    Location
    Behind you...
    Posts
    1,016
    I can't seem to duplicate what you are describing.

    I created 4 pages for my test, a main (initial page) and 3 sub pages that are linked from this main page. I click on the link for page 1, then use the back link. Then I click the link for page 2 and then use the back link again. Lastly I click the link for page 3 and then use the back link. Each time I'm brought to the correct page (my initial main page). Even if I change up the order and try any number of times I can't seem to get the incorrect history.

    Is there anything else you can show/share regarding this? Perhaps snippets from each of the pages you are using?

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles