www.webdeveloper.com
Results 1 to 5 of 5

Thread: Repeat HTML code in multiple pages (js/angular)

  1. #1
    Join Date
    May 2013
    Posts
    3

    Repeat HTML code in multiple pages (js/angular)

    Sup people.
    I'm making the visual part of my web application, all developed in javascript with the support of bootstrap and angularjs.


    The "site" contains fixed stuff, like a complex navigation bar, that is composed by lot of lines of code, that got repeated in each page.

    I'd obviously like to avoid code repetitions, both for ease of change (if i'd like to change a voice in my navbar i should change it in all the pages, argh!), so i was thinking to use something to memorize my code and repeat it while loading the page.

    My choice would be absolutely javascript, no php and if possible no jquery.

    I don't really know how to do it, i should declare a function that, if called, loads my snippet but i really don't know how this should be handled!

    Thank you very much

  2. #2
    Join Date
    Dec 2012
    Posts
    170
    It doesn't really make sense to do something like this using JavaScript, you should only use JavaScript for dynamic sections of a website that change while the user is viewing the page. It makes much more sense to use PHP, or an equivalent, to include common elements into a page, you could have a separate HTML page that just contains the navigation which you then include on all your page with the include function.

    PHP Code:
    include('navigation.html'); 
    Of course it would be possible to use JavaScript, but it would be pretty awkward.

  3. #3
    Join Date
    May 2013
    Posts
    3
    Quote Originally Posted by iBeZi View Post
    It doesn't really make sense to do something like this using JavaScript, you should only use JavaScript for dynamic sections of a website that change while the user is viewing the page. It makes much more sense to use PHP, or an equivalent, to include common elements into a page, you could have a separate HTML page that just contains the navigation which you then include on all your page with the include function.

    PHP Code:
    include('navigation.html'); 
    Of course it would be possible to use JavaScript, but it would be pretty awkward.
    Ok, so i'm really into php..
    i could invoke this script inside my html page?
    it would grant full compatibility with every device?

  4. #4
    Join Date
    May 2013
    Posts
    3
    By the way, my html page is something like:

    <html>
    <header>..</header>
    <body>
    <?php include('mypage.html'); ?>
    ..
    </body>
    </html>

    with mypage.html in the same folder, but it doesn't seem to work..

  5. #5
    Join Date
    Dec 2012
    Posts
    170
    PHP scripts will only work if the page they're on has the PHP extension, such as "index.php" and are being run on a server which supports PHP.

    You can install a local PHP server for testing PHP websites using xampp

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles