search

Home  >  Q&A  >  body text

javascript - Multiple pages share the same navigation. How can this shared navigation be introduced to avoid duplication?

A very common scenario. In the past, PHP's include was used to introduce it. What methods can be used at the front desk to do this?
The libraries currently introduced include jQuery and Webpack

Things that come to mind are:

  1. Send an Ajax request and put the requested page into the container:

    $.get("header.html",function(data){
       $("#header").html(data);
    });
  2. Make the navigation a jQuery component and initialize each page used

    `$("#header").initHeader()`
    

Are my two ideas correct?
What other methods are there?
And what are the commonly used methods?

PHPzPHPz2750 days ago860

reply all(2)I'll reply

  • PHP中文网

    PHP中文网2017-05-19 10:39:36

    There should still be a template file. It feels weird to implement it using jq components...

    reply
    0
  • 世界只因有你

    世界只因有你2017-05-19 10:39:36

    Write nav into public js and initialize it there.
    No need to initialize in every page.

    reply
    0
  • Cancelreply