Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Jul 2012
    Location
    Mexico
    Posts
    45
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Best way to repeat an html code?

    I need to position an ubiquitous menu in some pages of my site. That is, writing code once and that code repeating in every page (only the pages I want).

    I don't know if to use a function in javascript or this can be done in html and what is the best way to do it. Can you enlighten me please?

  • #2
    Senior Coder
    Join Date
    Aug 2006
    Posts
    1,444
    Thanks
    11
    Thanked 309 Times in 308 Posts
    Typically this is done with a server side include, either PHP (look up "php include") or SSI (look up SSI). You drop the menu code in a separate file, and "include" it in the various places you need it.

    Dave

  • #3
    Regular Coder Lerura's Avatar
    Join Date
    Aug 2005
    Location
    Denmark
    Posts
    947
    Thanks
    0
    Thanked 130 Times in 129 Posts
    if you are already using javascript, you can just as well use Ajax:

    Code:
    function InsertFile(File){xmlhttp = new XMLHttpRequest();xmlhttp.open("GET",File,false);xmlhttp.send();document.write(xmlhttp.responseText.toString());}
    Write the complete menu in a txt file and then place
    Code:
    <script>InsertFile(Whatever.txt);</script>
    in the document tree, where you want the menu to appear.

  • #4
    The fat guy next door VIPStephan's Avatar
    Join Date
    Jan 2006
    Location
    Halle (Saale), Germany
    Posts
    8,927
    Thanks
    6
    Thanked 1,041 Times in 1,014 Posts
    Quote Originally Posted by Lerura View Post
    if you are already using javascript, you can just as well use Ajax […]
    This is not a good recommendation, I’m afraid, because it makes it seem like it was OK to use JavaScript for everything without care (especially for newbies that don’t know any better). However, the “natural” state of a website is without JavaScript, and it has to work without JavaScript before you can add it to enhance the website’s functionality. Never rely on JavaScript for essential things in a website.

  • #5
    Regular Coder LearningCoder's Avatar
    Join Date
    Jan 2011
    Location
    The Pleiades
    Posts
    925
    Thanks
    76
    Thanked 29 Times in 29 Posts
    Ah yes, I remember when I was attempting to validate my form with just javascript at one point. Someone asked: "What would happen if the user turned JavaScript off in their browser?".

    That led me onto learning some php which is server-side validation for my forms.

    sorry to crash in on the topic, but I always wondered when validating a form with both JS and PHP, is there a possibility for the scripts to 'clash'?

    Kind regards,

    LC.
    Last edited by LearningCoder; 08-06-2012 at 12:07 PM. Reason: changed question

  • #6
    The fat guy next door VIPStephan's Avatar
    Join Date
    Jan 2006
    Location
    Halle (Saale), Germany
    Posts
    8,927
    Thanks
    6
    Thanked 1,041 Times in 1,014 Posts
    Quote Originally Posted by LearningCoder View Post
    sorry to crash in on the topic, but I always wondered when validating a form with both JS and PHP, is there a possibility for the scripts to 'clash'?
    Not if you don’t let them to, because JS is executed in the browser and PHP on the server. With form validation you validate the form with JS when the user hits the submit button, preventing the default submit action if something is wrong. So the PHP never even gets to see the form. Now, of course the validation logic has to be the same in JS and PHP. If JS allows a field that PHP forbids then PHP would just return the page with the error. But I suppose that’s not quite “clashing” since it would still work, basically. It becomes difficult if you pass a JS variable or whatever to PHP and PHP doesn’t handle it correctly, or vice versa. But while developing a functionality you’ll notice such things unless you’re not testing at all.

  • #7
    Regular Coder
    Join Date
    Aug 2005
    Location
    MS
    Posts
    851
    Thanks
    11
    Thanked 79 Times in 77 Posts
    Quote Originally Posted by juliushg View Post
    I need to position an ubiquitous menu in some pages of my site. That is, writing code once and that code repeating in every page (only the pages I want).

    I don't know if to use a function in javascript or this can be done in html and what is the best way to do it. Can you enlighten me please?
    If you have a good web editor, you can add code to multiple pages and sub-folder pages easily. By good, I mean some are free, but the better ones are paid versions. I use a copy/replace all operation for replacing or adding code to hundreds of files at once. Can be done only to currently open documents, can be done by selecting folder to scan and/or click to add sub-folder(s) files. Mine tells me how many files were done so if you know the total is correct, you are good to go. There's a technique to getting it done correctly depending on how your files are coded for the find to match on all pages you want updated. Option, is to use a web editor's code snippets and write the code you want and just open each page to paste snippet. A killer if you have lots of pages.
    ☠ ☠RON☠ ☠


  •  

    Posting Permissions

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