简体   繁体   中英

Dynamic Href not working using Javascript

function setLastLocation() {
        var lastLocation = window.location.pathname
        localStorage.setItem('lastLocation', lastLocation)
      }

Along with

function getLastLocation() {
        let lastLocation = localStorage.getItem('lastLocation')
        lastLocation = `../..${lastLocation}`
        return lastLocation
      }

<a href="javascript:getLastLocation()" class="close item item-1">
    <i class="fas fa-arrow-left fa-2x" style="color: white"></i>
</a>

Trying to make a back button for my cordova app to navigate to the previous page, but the href setup I have is not working the way I thought it would. There are two ways to get to the books of my app, by clicking on links from the search results or going to the books manually though the UI navigation. Depending upon which way the user gets to the books, I would like the href to redirect accordingly. The setLastLocation() is set on all the links leading to the content while the getLastLocation() is set on the back button of the actual content. When I type in the path for href literally (../../tracts.html), it works, but when I try to use the function in the href it doesn't work. Any suggestions? Here is a screen recording of the issue, link .

Instead of

href="javascript:getLastLocation()"

do

onclick="window.location = getLastLocation()"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM