0

If I am on a page and click the respective link in the navigation bar, how can I prevent the link from reloading the page (i.e. do nothing) when I am currently at that location? I still want the link to function, but only do nothing when I am already on that page.

Wallace Mogerty
  • 335
  • 1
  • 5
  • 16
  • is this vanilla JS, Jquery, angularJS, angular? – rjustin Sep 14 '17 at 20:54
  • @rjustin I would prefer to use just html if at all possible – Wallace Mogerty Sep 14 '17 at 20:55
  • 2
    Possible duplicate of [HTML Links that do nothing when you are on the page they link to](https://stackoverflow.com/questions/13119401/html-links-that-do-nothing-when-you-are-on-the-page-they-link-to) – JSON Derulo Sep 14 '17 at 20:55
  • If its just html, put a # in the href="#" on the page thats linking. If there is no scripting you will always know what page you are on. – rjustin Sep 14 '17 at 20:57
  • not possible with just html... you have to manipulate the DOM with some sort of script. – Si8 Sep 14 '17 at 20:57

1 Answers1

1

This is by far not the "best" way to do it I'm sure, but can be accomplished with Javascript/JQuery as I've done in the past when required in a mobile web application.

var url = window.location.pathname,
urlRegExp = new RegExp(url.replace(/\/$/,'') + "$");
$('a').each(function(){
    if(urlRegExp.test(this.href.replace(/\/$/,''))) {
        $(this).click(function(event) {
            event.preventDefault();
        });
    }
});

If PHP is an option you could try something like this:

\\Run this at top of page
<?php

        function getPageName()
        {
            $filename = $_SERVER["SCRIPT_NAME"];
            $breakfile = Explode('/', $filename);
            $pfile = $breakfile[count($breakfile) - 1];

            return $pfile;
        }

    ?>

\\Modify HTML links to something like this    
    <?php if(getPageName() == 'FILENAME.html'){ ?>

        \\Nav Link Here
        <a href="#" >NAV TITLE</a>


    <?php }else{ ?>


    <a href="FILENAME.html" >NAV TITLE</a>


    <?php } ?>
Dustin Snider
  • 678
  • 1
  • 8
  • 29