0

I've tried so many different variations and still can't get it to work. I'm just putting the prompt in there to make sure that the code is working as intended, and then the goal was to put more items into the if statement, but I can't get it to work initially. I've put the confirm function everywhere I can in the the js file and it doesn't work, but then if I put it on the actual index.php page with script tags it works just fine. I'm at a loss. This code is located in the js file.

$(document).ready(function() {

$(function (){


    $('#main-menu ul li a').each(function(){
        var path = window.location.href;
        var current = path.substring(path.lastIndexOf('/')+1);
        var url = $(this).attr('href');

        if(url == current){
            $(this).addClass('active');
            confirm(url);
        };


    });         
});

});

ETA: removed the document.write. that was just another test. here's the markup on the page

<div id="main-menu"><ul>
           <li id="home-link"><a href="index.php">Home</a></li>
           <li><a href="about.php">About</a></li>
           <li><a href="process.php">Process</a></li>
           <li><a href="portfolio.php">Portfolio</a></li>
           <li><a href="interiordesignnews.php">Press</a></li>
           <li><a href="http://domain.com/blog/" target="_blank">Blog</a></li>
           <li><a href="contact.php">Contact</a></li>
       </div></center>
Angie
  • 25
  • 5
  • 2
    You don't need both `$(document).ready()` and `$(function() { ...});`. I usually just use `$(function() { ... });` by itself. – vbguyny Nov 17 '16 at 20:42
  • seems like the problem is either the script is not being included in the page, or possibly that your selector is wrong. show the markup. – I wrestled a bear once. Nov 17 '16 at 20:45

1 Answers1

2

document.write should no longer be used. It's a deprecated feature of JavaScript bad practice.

In your case, since you are running it after the DOM has loaded, it will erase your entire page and then write "lorem". Once that happens, your '#main-menu ul li a' element(s) will no longer exist.

Also, $(function(){ }); is shorthand for $(document).ready(function(){}), so there's no reason to be using both.

Community
  • 1
  • 1
gen_Eric
  • 223,194
  • 41
  • 299
  • 337