javascript - jQuery .load: any way to load the same page when page is refreshed


Question: 

So I have a website that loads pages to a container div:

function goto(addr) {
    $("#content").load(addr);
}

and a link that executes it

<a href="#" id="aboutus" onclick="goto('page/aboutus.php');">About us</a>

My problem is that whenever the page is refreshed, the loaded content resets to the default page (page/home.php). How could I do so that it loads the previous displayed page?




2 Answers: 

Use local storage for example or sessions. Local storage example:

$(document).ready(function() {

    var lastPage = localStorage['lastPage'];
    if (!lastPage) { // If user was on any url before we will exectue goto function
        goto(lastPage)
    }

    function goto(addr) {
         localStorage['lastPage'] = addr; // Set url to local storage before load page
         $("#content").load(addr);
    }

});
 

not only localStore but you need change the hash of url, and after do one function to catch hash code and execute at you "goto" function...

"something like that"

function hashnav(){
   var hashfull = document.location.hash
   var hash = hashfull.replace('#', '');
   var $page = '';
   goto(hash);
}
function changeHash($hash) {
   window.location.hash = $hash;
}

function goto(addr) {
   changeHash(addr);
}

$(window).bind( 'hashchange', function() {
   hashnav();
   return false;
});
 

More Articles


email - Getting Mail body In PHP

What I am doing:First I am trying to get email and save the reply of user in my database.For this I am using PHP imap method to get mail from my email address.Code:function get_email(){ //define server, email , password $server = '{server.example.com:143}INBOX'; $user_name = 'login';

Facebook Like Button: Unify a Url & a Facebook Page

In Facebook docs it says:Can I link the Like button to my Facebook page?Yes. Simply specify the URL of your Facebook page inthe href parameter of the button.My problem is that I don't want the Facebook page to be the target of the 'like'.So currently, the like button on my site attributes the likes

functional programming - Mix pure function with side effect in scala

I have a simple api method:def addWishToBoard(wish: Wish, boardId: BoardId, userId: UserId): Option[Board]It adds wish to board, if boardId and userId matches.Boards are stored simply in a list:private var boards = List.empty[Board]I use var here in order to simulate side effect.The implementation c


php - get_footer not showing for Wordpress site

For some reason my footer disappeared for my site http://juliannaspizza.com. I checked the template file for the home page and get_footer is being called as shown below. Any idea why my footer is still not showing up? <?php /** * Template Name: Full Width Page * * @package Fortune */ get_he

django - R_user not defined , rpy2

I want to use R from django app .Now I am in a huge mess .--> I have installed rpy2 for that .---> I am able to run everything from Python IDE eg . import rpy2.rinterface as rinterfacerinterface.initr()or import rpy2.robjects as somethingAs I open Python from cmd or in django file .I am getting err

specifications - Essential techniques for pinpointing missing requirements?

An initial draft of requirements specification has been completed and now it is time to take stock of requirements, review the specification. Part of this process is to make sure that there are no sizeable gaps in the specification. Needless to say that the gaps lead to highly inaccurate estimates,


reactjs - What is the benefit of @emotion/core over emotion for a React project?

If I install emotion then the API is nice and clear: package.json:"dependencies": { "emotion": "^10.0.9",React component: import React from "react";import { css } from "emotion";const someStyle = css` display: none;`function MyComponent() { return ( <div className={someStyle} /> );}Ho

php - how to set a page active automatically when it is loaded

i want to change the active class when each page loaded. my jquery code is$(document).ready(function () {$('.nav li a').click(function(e) { $('.nav li a.active').removeClass('active'); $(this).addClass('active'); e.preventDefault();});});and my html code is<ul class="nav nav-stacked bg-n

macos - uninstall python on mac Mavericks

I'm a new Mac user. I installed python 2.7.6 but I cannot run my scripts (through IDLE or simply a script). I want to reinstall it but I don't want to mess with the version which comes pre-installed on mac.Thank you for your help.note: there are questions on stackOverflow that are similar to mine, y

R: turning list items into objects

I have a list of objects that I've created manually, like this:rand1 <- rnorm(1e3)rand2 <- rnorm(1e6)myObjects <- NULLmyObjects[[1]] <-rand1myObjects[[2]] <-rand2names(myObjects) <- c("rand1","rand2")I'm working on some code that bundles up objects and puts them up in S3. Then I ha