4

Is it possible to go to a different page than the current window.location.href of reload?

I'm trying to do it this way with a jQuery event handler on window, but it doesn't seem to be working.

$(window).on('reload', function(event){
    event.preventDefault();
    var currentURL = window.location.href;
    window.location.href = currentURL.split('!')[0];
});
wordSmith
  • 2,993
  • 8
  • 29
  • 50
  • 2
    If you trying to prevent double posting, then there's better ways to do it. If not, care to explain what are you trying to do with this? – fejese Dec 20 '14 at 00:53
  • 3
    There is no "reload" event. – epascarello Dec 20 '14 at 00:57
  • You have to use the unload event and check if the page has been loaded before. You can see more details here: http://stackoverflow.com/questions/10400182/how-to-check-page-is-reloading-or-refreshing-using-jquery-or-javascript – Greg Dec 20 '14 at 00:53
  • do you have the information (url) on where to go at reload time? – blurfus Dec 20 '14 at 01:48
  • @fejese No. I'm using the history api to manipulate the URL to pages that don't exist. So on reload, I want it to go to the initial page that does exist. – wordSmith Dec 20 '14 at 02:08
  • @epascarello oh. I thought there was. Why am I trying to do this? I'm using the history api to manipulate the URL to pages that don't exist. So on reload, I want it to go to the initial page that does exist. – wordSmith Dec 20 '14 at 02:08
  • 1
    You should not fake the existence of a URL. If you populate the history with an entry, then make sure that your application can load that page. Otherwise what's the point? In case you navigate elsewhere and hit browser back button you'll fail to load the page. – fejese Dec 20 '14 at 02:12
  • @wordSmith if the initial page does not exist, you would get a 404 (not found) error - are you planning on putting this code on your own 404 page? – blurfus Dec 20 '14 at 17:01
  • @ochi Yes, I do get the 404 error. I could redirect from there! Thanks! – wordSmith Dec 23 '14 at 15:15

3 Answers3

0

Assuming the original URL was of the form <orig_url>?|<new_url> you could do this on button click

$(document).ready(function () {
    $('.btn').on('click', function () {
        // Original assumption is the original URL was of the form <orig_url>?|<new_url>

        // faking our data here for this example
        var currentURL = window.location.href + '|http://www.google.com' ;

        console.log(currentURL );
        var urls = currentURL.split("|");
        console.log( urls[0] );
        console.log( urls[1] );
        $("head").append("<META HTTP-EQUIV='Refresh' CONTENT='0;URL=" + urls[1] + "'>");
    });
});
blurfus
  • 13,485
  • 8
  • 55
  • 61
  • I think this is too complicated. Here's why I'm trying to do this: I'm using the history api to manipulate the URL to pages that don't exist. So on reload, I want it to go to the initial page that does exist. – wordSmith Dec 20 '14 at 02:09
0

Try this one.It will fixed the problem.

 $( window ).load(function() 
      {
          var your_url         = 'your_page.php'; 
          window.location.href = your_url;
      });
Raham
  • 4,781
  • 3
  • 24
  • 27
-1

since you are trying to control page load behaviour at client side you need to have some mechanism to prevent this information at browser. You can make use of cookie.

You may reuse below code.

function setCookie(c_name, value, exdays) {
            var exdate = new Date();
            exdate.setDate(exdate.getDate() + exdays);
            var c_value = escape(value) + ((exdays == null) ? "" : "; expires=" + exdate.toUTCString());
            document.cookie = c_name + "=" + c_value;
        }

function getCookie(c_name) {
    var i, x, y, ARRcookies = document.cookie.split(";");
    for (i = 0; i < ARRcookies.length; i++) {
        x = ARRcookies[i].substr(0, ARRcookies[i].indexOf("="));
        y = ARRcookies[i].substr(ARRcookies[i].indexOf("=") + 1);
        x = x.replace(/^\s+|\s+$/g, "");
        if (x == c_name) {
            return unescape(y);
        }
    }
}

function DeleteCookie(name) {
        document.cookie = name + '=; expires=Thu, 01-Jan-70 00:00:01 GMT;';
    }



$(window).load(function () {
 //if IsRefresh cookie exists
 var IsRefresh = getCookie("IsRefresh");
 if (IsRefresh != null && IsRefresh != "") {
    //cookie exists then you refreshed this page(F5, reload button or right click and reload)
    DeleteCookie("IsRefresh");
    //Redirect to new URL
 }
 else {
    //cookie doesnt exists then you landed on this page for first time
    setCookie("IsRefresh", "true", 1);
 }
})
Amit Anand
  • 85
  • 1
  • 1
  • 11