...

View Full Version : Best way to repeat an html code?



juliushg
08-05-2012, 07:35 AM
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?

tracknut
08-05-2012, 04:00 PM
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

Lerura
08-06-2012, 07:23 AM
if you are already using javascript, you can just as well use Ajax:


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

<script>InsertFile(Whatever.txt);</script> in the document tree, where you want the menu to appear.

VIPStephan
08-06-2012, 10:34 AM
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.

LearningCoder
08-06-2012, 11:06 AM
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.

VIPStephan
08-06-2012, 01:01 PM
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.

Major Payne
08-06-2012, 06:09 PM
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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum