...

View Full Version : Html into a javascript file?



Andy92
04-28-2006, 08:39 AM
Hi there,

In my html files i have put this in...

<script type=text/javascript src=myfile.js></script>

Then in the myfile.js i have wrote...
document.write("hey");
And in the html file it puts in "hey".

But my question is how do i put all of the following html code below into a javascript .js file ?


<div id="left_col">
<div>
<div class="text_menu_title">News:</div>
<div class="text_menu"> &raquo; <a href="n">Newest</a></div>
<div class="text_menu"> &raquo; <a href="a">Adventure</a></div>
<div class="text_menu"> &raquo; <a href="f">Fun</a></div>
<div class="text_menu"> &raquo; <a href="r">Reviews</a></div>
<div class="text_menu_adds_title">Adverts:</div>
<div class="text_menu_adds"> &raquo; <a href="s">Starlinkuk Hosting</a></div>
<div class="text_menu_adds"> &raquo; <a href="c"></a><a href="t">Things 4 / Great Gifts</a></div>
<div class="text_menu_adds"> &raquo; <a href="hh">Advertise Here?</a></div>
</div>
</div>

:confused: :confused: :confused:

Kravvitz
04-28-2006, 09:24 PM
Don't. Keep in mind that some Internet users use a browser that doesn't have JavaScript enabled (http://www.thecounter.com/stats/2006/March/javas.php) and that search engines don't support JavaScript.

You should use PHP includes (http://www.joe2torials.com/view_tutorial.php?view=3) or SSI (http://httpd.apache.org/docs/howto/ssi.html) instead.

Also, you really should use an unordered list to mark that up instead of using so many <div>s.
These three articles explain how to style lists.
http://www.alistapart.com/articles/taminglists/
http://css.maxdesign.com.au/listutorial/
http://www.webreference.com/programming/css_lists/

Don't make a common newbie mistake -- the point of table-less layouts is not to simply replace tables with <div> elements. The point is to use semantically correct markup and then use CSS to make it look the way you want it to.

roxii
04-28-2006, 11:16 PM
I thoroghly agree with Kravvitz about everything in his post, but it's still good to know how to do it in JS.


document.write('<div id="left_col">');
document.write('<div>');
document.write('<div class="text_menu_title">News:</div>');

And so on.

To do it in PHP, create a file called myfile.php and put in the menu HTML/XHTML as is inside it.

To call it on your website, use this:


// The HTML/XHTML before the menu should appear
<?php
include "myfile.php";
?>
// The bottom half of your HTML/XHTML

To use PHP, you must use the PHP extension on the page itself, so if your page was index.html, rename it to index.php.

Andy92
04-29-2006, 10:10 AM
Right,

Ive found this thing that you type in html code and it converts it to javascript for you...

http://javascript.about.com/library/blscr02.htm

Also its kind of the same method of inserting things like php include and javascript.

But what one is better? Shall i include the javascript files in all my pages or shall i include php files instead of javascript files?

What one is better?

Also, what do you mean by some search engines cant pick up javascript?

Can all browsers pick up php? And if i did it in php would it be better?

Also with javascript you have all the document.write stuff. In the php file that i want to attach to all my pages, couldnt i just put the <html></html> tags in the php file and put all my html in there and it would insert it?

Andy92
04-29-2006, 10:26 AM
Also, i have just tested out the...

<?php
include "file.php";
?>

And it seems to work fine, but why do you have to rename your page index.php rather than index.htm ?

Also, when i did it the javascript way it took longer to load, the php does it instantly.

Another diffarance is that when you put in your index.htm <script type=text/javascript src=myfile.js></script> , in the design mode, it doesnt let you know that anything is there, but when you do it the php way it comes up with this yellow box saying php.

But if all your pages are .php can you still include the <script type=text/javascript src=myfile.js></script> in the index.php file or does it have to be a .htm file?

Also, when you come to save your files it has...
.php
.php3
.php4
.php5

Mine are all .php at the moment, but whats the difference and whats the best one to go for?

roxii
04-29-2006, 08:53 PM
Renaming the page to .php tells the computer serving the website that there is PHP code on the page. If you didn't have the .php extension, you'd just end up with the PHP code being displayed as text when viewed in a browser. Having a page with the PHP extension does not affect anything else, so you JS and other things work as it would if it was .htm.

I'm not really up to scratch as to why one would rather use .php5 instead of .php, but just keep them as .php. The number indicates the version of PHP, but .php works for all of them.

Kravvitz
04-29-2006, 09:48 PM
But what one is better? Shall i include the javascript files in all my pages or shall i include php files instead of javascript files?

What one is better?
For this PHP is, without a doubt, better.


Also, what do you mean by some search engines cant pick up javascript?
Search engines ignore JavaScript because they haven't been programmed to understnad it.


Can all browsers pick up php? And if i did it in php would it be better?
PHP is run on the server. JavaScript is run in the user's browser. If you can do something on the server, instead of int he user's browser it's generally better to do it on the server.


In the php file that i want to attach to all my pages, couldnt i just put the <html></html> tags in the php file and put all my html in there and it would insert it?
I'm not sure what you're asking.

felgall
04-29-2006, 10:05 PM
PHP is the better alternative. It runs on the server and inserts the include file before sending the page to the browser so the browser never knows that there was an include used at all. The javascript solution should only be used when the content is not essential to the overall page (in case Javascript is not available in the browser) and where a server side option is not available (eg. you are using "free" hosting).

Andy92
04-30-2006, 03:15 PM
Ok, ill do it with php and let you know how i got on.:thumbsup:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum