简体   繁体   中英

HTML one href linking to two different pages depending on current page

I have a menu that is static and is the same on all page views. On the main page it should link to bottom of the page (#footer) and when you go to a subpage then it would link to another page. In my case I have a blog on Magento shop and on the front page there are some blog posts at the bottom of the site and when clicked on the main menu it should redirect there. But when user goes to category or product page then it should link to blog directly.

I'm not sure if this is even possible, google didnt help also. If there are any ways I would be happy to see them. Thanks!

Your solution would be like this:

<a href="#" id="myBtn">Click Me</a> 

<script>

$(document).ready(function(){

    $('#myBtn').click(function(){

        if(window.location.href == "http:// ...."){ //used to find the current page location 
            location.href = "#abc"; //at the bottom of page
        }
        else{
            location.href = "http:// .... ";//the next page you want to take the user to
        }

    });

});

</script> 

Using plain html, any anchor cannot have multiple href locations. But, by making use of JavaScript (I have used jQuery Lib) you can first find the current webpage location and based on it redirect the user on click of hyperlink or button to other locations on your desire.

Maybe you can use Javascript to get the page where you at. Then build an if-statement with something like if (page == homepage) to go to the footer and (if page == category go to product).

if sub-page is on different URL, Use condition by checking current URL.

Get current URL in web browser

  <script> 

function myFunction() {

var str = window.location;

var patt = new RegExp("#ide");

var res = patt.test(str);

if(res){

  location.replace("http://www.homepage.com") 

}else{

  location.replace("http://www.subpage.com#ide") 

}

}

may be this script will help you.

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