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 9 of 9
  1. #1
    Regular Coder Andy92's Avatar
    Join Date
    Nov 2005
    Location
    Horsham, West Sussex, UK
    Posts
    363
    Thanks
    7
    Thanked 1 Time in 1 Post

    Html into a javascript file?

    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>

  • #2
    Senior Coder
    Join Date
    Feb 2006
    Location
    USA
    Posts
    1,013
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Don't. Keep in mind that some Internet users use a browser that doesn't have JavaScript enabled and that search engines don't support JavaScript.

    You should use PHP includes or SSI 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.
    Learn CSS. | SSI | PHP includes | X/HTML Validator | CSS validator | Dynamic Site Solutions
    Java != JavaScript && JScript != JavaScript
    Design/program for Firefox (and/or Opera), apply fixes for IE, not the other way around.

  • #3
    New Coder
    Join Date
    Apr 2005
    Posts
    84
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I thoroghly agree with Kravvitz about everything in his post, but it's still good to know how to do it in JS.

    Code:
    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:

    PHP Code:
    // 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.

  • #4
    Regular Coder Andy92's Avatar
    Join Date
    Nov 2005
    Location
    Horsham, West Sussex, UK
    Posts
    363
    Thanks
    7
    Thanked 1 Time in 1 Post
    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?

  • #5
    Regular Coder Andy92's Avatar
    Join Date
    Nov 2005
    Location
    Horsham, West Sussex, UK
    Posts
    363
    Thanks
    7
    Thanked 1 Time in 1 Post
    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?

  • #6
    New Coder
    Join Date
    Apr 2005
    Posts
    84
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #7
    Senior Coder
    Join Date
    Feb 2006
    Location
    USA
    Posts
    1,013
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Allsortshop
    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.

    Quote Originally Posted by Allsortshop
    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.

    Quote Originally Posted by Allsortshop
    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.

    Quote Originally Posted by Allsortshop
    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.
    Learn CSS. | SSI | PHP includes | X/HTML Validator | CSS validator | Dynamic Site Solutions
    Java != JavaScript && JScript != JavaScript
    Design/program for Firefox (and/or Opera), apply fixes for IE, not the other way around.

  • #8
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,620
    Thanks
    0
    Thanked 645 Times in 635 Posts
    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).
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #9
    Regular Coder Andy92's Avatar
    Join Date
    Nov 2005
    Location
    Horsham, West Sussex, UK
    Posts
    363
    Thanks
    7
    Thanked 1 Time in 1 Post
    Ok, ill do it with php and let you know how i got on.


  •  

    Posting Permissions

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