javascript - $(document).ready firing before the DOM is loaded -


i'm working on nav bar using bootstrap 4.0. have pillbox nav bar , wrote javascript function automatically detect current page , add active class right pillbox. set function run when dom ready runs before of html in body loaded. script fails find elements 'nav-link' class @ runtime , nothing. if add async attribute script however, works expected.

function main () {  $nav = $('nav-link');  $('.nav-link').each( function () {     var currentpage = location.href.split("/").slice(-1);     var currentlink = $(this).attr('href').slice(2);     console.log(currentpage);     console.log(currentlink);      if (currentlink == currentpage) {          $(this).addclass('active');     } else {         $(this).removeclass('active');     }  });   }  $(document).ready(main()); 

this html file.

<html lang="en"> <head>     <!-- required meta tags -->     <meta charset="utf-8">     <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">     <title>home</title>     <!-- library cdns -->     <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>     <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.11.0/umd/popper.min.js"></script>     <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/js/bootstrap.min.js" integrity="sha384-h0abixch4zdo7tp9hkz4tshbi047nrkglo3sejag45jxxngifyzk4si90rdiqnm1" crossorigin="anonymous"></script>     <!-- custom js -->     <script async type="text/javascript" src='checknavactive.js'></script>     <!-- bootstrap css -->     <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.css" integrity="sha384-/y6pd6fv/vv2hjna6t+vslu6fwyxjcftcephbnj0lyafsxtsjbbfadjzaleqsn6m" crossorigin="anonymous">     <!-- custom css -->     <link rel="stylesheet" href="./wikicss.css"> </head>  <body>  <div>     <ul class="nav nav-pills">       <li class="nav-item">         <a class="nav-link" href=".\home.html">home</a>       </li>       <li class="nav-item dropdown">         <a class="nav-link dropdown-toggle" href=".\drylab.html" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">drylab</a>         <div class="dropdown-menu">           <a class="dropdown-item" href=".\drylab.html" >drylab</a>           <a class="dropdown-item" href="#">another action</a>           <a class="dropdown-item" href="#">something else here</a>         </div>       </li>       <li class="nav-item">         <a class="nav-link" href=".\wetlab.html">wetlab</a>       </li>     </ul> </div>   <div class="container title">     <div class="row">         <div class="col text-center">             <h1>home</h1>         </div>     </div> </div>   </body> 

using $(document).ready(main()) calls main() function , there. should using $(document).ready(main)

function main() {      $nav = $('nav-link');      $('.nav-link').each(function() {      var currentpage = location.href.split("/").slice(-1);      var currentlink = $(this).attr('href').slice(2);      console.log(currentpage);      console.log(currentlink);        if (currentlink == currentpage) {        $(this).addclass('active');      } else {        $(this).removeclass('active');      }      });  }    $(document).ready(main);
<html lang="en">    <head>    <!-- required meta tags -->    <meta charset="utf-8">    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">    <title>home</title>    <!-- library cdns -->    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.11.0/umd/popper.min.js"></script>    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/js/bootstrap.min.js" integrity="sha384-h0abixch4zdo7tp9hkz4tshbi047nrkglo3sejag45jxxngifyzk4si90rdiqnm1" crossorigin="anonymous"></script>    <!-- custom js -->    <script async type="text/javascript" src='checknavactive.js'></script>    <!-- bootstrap css -->    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.css" integrity="sha384-/y6pd6fv/vv2hjna6t+vslu6fwyxjcftcephbnj0lyafsxtsjbbfadjzaleqsn6m" crossorigin="anonymous">    <!-- custom css -->    <link rel="stylesheet" href="./wikicss.css">  </head>    <body>      <div>      <ul class="nav nav-pills">        <li class="nav-item">          <a class="nav-link" href=".\home.html">home</a>        </li>        <li class="nav-item dropdown">          <a class="nav-link dropdown-toggle" href=".\drylab.html" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">drylab</a>          <div class="dropdown-menu">            <a class="dropdown-item" href=".\drylab.html">drylab</a>            <a class="dropdown-item" href="#">another action</a>            <a class="dropdown-item" href="#">something else here</a>          </div>        </li>        <li class="nav-item">          <a class="nav-link" href=".\wetlab.html">wetlab</a>        </li>      </ul>    </div>        <div class="container title">      <div class="row">        <div class="col text-center">          <h1>home</h1>        </div>      </div>    </div>      </body>


Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -