Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to refresh only a part of the page when F5 or refresh button is pressed

I'm designing a web application that has a shared menu for all pages. Due to this i decided to load the contents linked by the menu buttons inside a div, using jquery.

So, I have this:

     $("#AddNewProductBtn").click(function() {
       $("#content").load("addproduct.html");
   });

I want to keep track of the page displayed inside the "#content" div. If the users refreshes the page I want to load the same page in "#content". Is there any way to do this, or is there any workaround?

I saw some websites that use an iframe to load the pages, but when a user clicks a button in the menu the url is also changed. I didn't find any info on how to do that.

Thanks

like image 894
Dan Dinu Avatar asked Sep 24 '11 14:09

Dan Dinu


People also ask

How do I refresh only part of a web page?

Refreshing part of a page periodically You can use the frame “reload” function periodically in frameset page itself. In the above code, “right_frame” reloads every second. setInterval() function is very closely related to setTimeout() – both have similar syntax: setInterval ( expression, interval );

What is the difference between Ctrl F5 and F5?

F5 is a standard page reload. Ctrl + F5 refreshes the page by clearing the cached content of the page. Having the cursor in the address field and pressing Enter will also do the same as Ctrl + F5 .

How do I stop F5 refresh in a form?

we can easily disable f5 button using javascript with example. we will disable f5 key using keydown event of jquery. as we know f5 key code is a 116, so basically if user will press 116 event. keyCode then we will simply preventDefault with return false.


2 Answers

Here's a solution for the F5 and CTRL+R

$(document).keydown(function(e) {
    if (e.which == 116 || e.keyCode == 82 && e.ctrlKey) { //116 = F5
        $("#content").load("addproduct.html");
        return false;
    }
}); 
like image 74
Book Of Zeus Avatar answered Sep 19 '22 11:09

Book Of Zeus


You should append parameter to the hash part of the url (after #), for example domain.com/index.php#addproduct

Then on document.ready check the value after # and load the corresponding content.

Some plugins like jQuery history use this technique.

Additionally, you can leverage the local storage and cache parts of the code at the browser, so you won't load the content with AJAX call the second time.

like image 23
Maxim Krizhanovsky Avatar answered Sep 21 '22 11:09

Maxim Krizhanovsky