I'm working on a navigation bar similar to this website and google, and lets say i would like to make more sites with the same layout. How would i go about doing this with html? I know that php has:
<?php include "file name"; ?>
Is there a way to do something like that in HTML document?
HTML is not a server-side language, it's just a markup language. It does not offer any way to include the contents of a different page in your HTML code. HTML simply doesn't support it.
If you're willing to use a library such as jQuery, then this will be an easy task. You can use the jQuery .load() method to fetch the contents of your HTML file and then inject it the HTML DOM.
$(document).ready(function() {
$('#someDiv').load('/path/to/file.html');
});
Note that remote files won't work because of the Same Origin Policy. Also, note that it's a bad idea if you care about SEO. The search engine spiders won't see your actual HTML code, and they might miss out important parts of your website.
The best way to do this would be to use a server-side language. If you're already serving dynamic content, then it makes more sense to do so. Server-side scripting runs on the server machine and then the results of that scripting, generally HTML markup are then sent out to the client.
The difference is that the HTML markup is generated before pageload, not during or after, as is the case with the method shown above.
Just make your HTML files run through a PHP parser, I do that for all my sites so things like menus and other repeating sections can just be updated once. You do this with your .htaccess file.
This will make all .htm and .html files be run through the PHP parser as well.
<FilesMatch "\.(htm|html)$">
SetHandler application/x-httpd-php
</FilesMatch>
Then you just use the same method you mentioned in your original post. Whatever you include will be processed by the PHP parser, then sent to the browser, which will interpret the HTML.
Note that PHP files can hold anything in them, only the parts inside <?php ?> tags will actually be processed by the PHP parser. So I have section that are all HTML, except for one small section that has something like a year. The below is how my websites will never have their copyright be out of date, even if the website is not maintained.
<p>
Copyright<sup>©</sup> NAME. All Rights Reserved. <?php echo date("Y"); ?>.
</p>
"Is there a way to do something like that in HTML document?"
Yes there is an HTML equivalent (if I can say) to include files, and it's called Server-side Includes (SSI) which uses the .shtml file extension, however this is an Apache feature. Yet, from a recent finding, it can run on Windows servers. Consult Microsoft's Developer Network for more information on the usage of SSI and this page also on Microsoft's IIS.net.
The syntax is: (no space between the <!-- and the #include...)
<!--#include virtual="/includes/file.shtml" -->
This would look and fetch a file called file.shtml to the root and inside the "includes" folder.
Note: This can be any folder you want it to be.
You can also include different file extensions such as .htm .html .txt and even another .shtml file.
I manage a few Websites with that particular file extension, and the benefit of including .shtml files, is that you can also do more includes inside those, but not with the other file extensions I already listed.
However, there is an exception to this rule. You can tell the server to treat .html or .htm to run as .shtml just as long as you use the
AddHandler server-parsed .html
AddHandler server-parsed .shtml
AddHandler server-parsed .htm
Apache commands inside an .htaccess file and placed in the root of your server.
Back to the matter at hand. You can use it anywhere in the document you wish to include a file in.
For more information on Server-side Includes and other available options, visit the Wikipedia Website, or Google the term "Server-side Includes (SSI)".
http://en.wikipedia.org/wiki/Server_Side_Includes
<frameset cols="25%,50%,25%">
<frame src="www.google.com">
<frame src="www.facebook.com">
<frame src="www.something.ocm">
</frameset>
FRAMETAG is not supported in HTML5! Beaware
In W3C Validation it will throw an error, but in real it will work.
Related
For some sites that have their backend on PHP/any other server side languages, the query string http://example.com/?param1=whatever makes sense. But I saw it is used with pages that have .html extension as well. How does that make sense? If we are explicitly writing the url to end in .html, it means it is referring to a file located on the server, and it is an HTML file, which cannot deal with server side stuff. So how does it make sense?
As an example, http://www.example.com/mypage.html?crcat=test&crsource=test&crkw=buy-a-lot1, this url is given on https://help.marketruler.com/wiki/What_is_the_correct_syntax_for_query_strings%3F link (under Landings to a page within your domain name with no query string).
There are two reasons:
You can fetch the parameters with JavaScript and use them with client side programming
You can change the Mime type of .html files and parse them as a PHP or other programming language script. For example in Apache to make html files to work like php scripts it is as simple as adding this to .htaccess file:
AddType application/x-httpd-php .html
I have a piece of e-commerce software that is purely Html based. I want to create the functionality of logging in and having an account.
I can't use PHP or mysql due to .html not being able to process it. I can't change them to .php as the software wouldn't function correctly.
Is there any other way around this? How is it possible to have this functionality and also is it even possible?
Is there a way to make php sit over the top of the html controlling the log in feature somehow?
My host has Php and mysql installed, but I simply cant use it with my webpages
You can do this by telling Apache to process files with a .htm or .html file extension as PHP files. This will allow you to add php to your .html files to build a login and user account system effectively over the top of your existing site.
To do this you need to create a .htaccess file in your root web directory and add this line to it:
AddType application/x-httpd-php .htm .html
Then simply add the php tags to you .html pages with the desired php code.
<?php
// your php code
?>
Given the following index.html file:
<html>
<body>
<p>Welcome to <?= $_SERVER ['HTTP_HOST']; ?></p>
</body>
</html>
I expect to see "Welcome to EXAMPLE.COM", All I see is "Welcome to ".
What would cause this to happen if the code checks out?
Your file is named index.html. Unless you told your server that .html files should be treated as PHP scripts, that means the PHP code is NOT being executed - it's going out as literal text. And since PHP tags make it look like HTML, your browser is properly hiding that unknown/illegal tag.
Rename it to index.php.
It's not running because that part is not html (it's php) but you have saved it as an html file instead of as a php file.
rename it to index.php and try again?
Web servers are usually configured to run PHP only on files with the .php extension. Your index.html file will be passed as-is to the browser, which will probably ignore the unknown PHP tags. If you take a look at the source code of the web page, the tags will probably be there.
If you must have a .html extension, you can usually configure the web server to run PHP on .html files. For example, in Apache, you can use the AddType directive in an .htaccess file or in the server configuration (httpd.conf):
AddType application/x-httpd-php .html
However, this will run PHP on all .html files (in that directory), which may put an unnecessary load on the server.
A much better way is to use URLs without extensions. In Apache, you can use the DirectoryIndex directive to specify a list of index files that the web server will search for:
Options +Indexes
DirectoryIndex index.html index.php
When a browser requests a URL that ends with a slash, such as http://mydomain.example/foo/, the server will search for foo/index.html or foo/index.php in the DocumentRoot (or, failing both, generate a directory listing). You can now use whichever type of index file is appropriate for the moment, without ever having to change your URLs.
I've searched all over the internet and can't seem to find a solution to my problem. I want to be able to "call" a php file from an html file and display the string returned:
html_ONLY_file.html
...
<h1>GetHeader.php?type=main</h1>
...
GetHeader.php
if($_GET['type'] == 'main')
print 'Some header to display'; //or echo 'Some...';
exit;
I've done this for images, img src="image.php?file=file.jpg", where image.php does a header(...) and readfile(...) return but I do not know how to do this for simple text. I'm not looking for DOM or anything too involved, if I have to I will. I want to know if there is a simple solution. It's generating the html side that I'm lost on.
In case you want to know, I am doing this because I once used a <#virtual include=...> to call for a php file from my .shtml file. Well, the hosting company decided to change mod_security and now I cannot include any php files (I've already tried everything). Including html files works fine and so I am changing this part of the website around so I don't have to rename files because the site is for a small business that is now ranked highly on Google for its geographical area. If I change the file names, shtml to php, then I believe the Google ranking drops (don't comment on this part unless you are damn sure you are 100% correct).
If you can edit the .htaccess file you can add a line in your .htaccess file that will mean HTML files will be parsed as if they were PHP.
If your server is running PHP as an apache module:
AddType application/x-httpd-php .html .htm
If your server is running PHP as CGI:
AddHandler application/x-httpd-php .html .htm
Source: http://www.besthostratings.com/articles/php-in-html-files.html
Once you've added that your html_ONLY_file.html could look like:
<h1><?php print "some header"; ?></h1>
And it would function just as if it were a .php file.
Alternatively, you could convert all your files to .php and add redirects into the .htaccess file like so:
rewriteRule ^somefile.shtml$ somefile.php [R=301,L]
rewriteRule ^another-file.shtml$ another-file.php [R=301,L]
This effectively says to your server "whenever a user requests somefile.shtml, act as if they requested somefile.php instead". The R=301 is the most important part with regards to Google rankings. This tells Google (and anyone who requests the .shtml file) that it's permanently moved to the new location somefile.php. This transfers all / almost all of the ranking power from the old location to the new location.
Source: http://moz.com/learn/seo/redirection
Okay, so I'm very new to website design, so please keep that in mind.
I've been working on a website (with file extension .shtml), and the time has come to add php code to it. It's only a little code, but it doesn't work on the .shtml website; the php code ends up in the source code as a comment. I was wondering if there was a way to make php code work on an shtml website.
Thanks.
Change the .shtml to .php update the include files you probably have and your on your way.
To keep your .shtml add this
AddType application/x-httpd-php .shtml
Keep in mind this will put additional load on your webserver as it will now scan all .shtml for php instead of just .php files
This can be added in the httpd.conf file
Write your PHP code as a separate .php file and then include it using SSI, as so:
<!--#include virtual="/path/to/yourfile.php" -->
This worked for me on a hosted website with no changes to .htaccess required. The PHP executes before being included in the .shtml page.
You'll need to setup a web server to get this working. Checkout Apache, or http://www.apachefriends.org/en/xampp.html for something very easy to setup. You can't just drop PHP into an html page and expect it to execute, it needs the interpreter.