Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Sep 2010
    Thanked 0 Times in 0 Posts

    putting code snippets into html

    Hi, I'm just now starting to try to insert some javascript into my html and php code. I'm only used to working with those two markups. I need to try some very simple javascript snippets in some forms I'm making, but am having an issue getting the code to work.

    When I find these snippets, they are usually in a format like this:
    <SCRIPT LANGUAGE="JavaScript">
     // functions and whatnot here  
     function makeSomethingHappen(field, count, max) {
     if (something.works > true)
     field.value = i.will.crap(0, max);
     // calculate the remaining characters  
     field.value = max - more.nodoze.caffein;
     <form name=sample action="">
     <input type="text" name="hours">
    I've tried inserting the stuff like the webpages have it listed. I also try inserting everything contained in the <script> tags into the head of the document (or creating a head and putting it in there, since all my stuff is php and html code called to the file with php includes and whatnot).

    Anyways, I hoping somebody can provide a pointer as to how to use these snippets or how to correctly place things like this. I'm assuming there's a relatively uniform way that people are posting their tiny little js codes and I'm missing some basic knowledge as to how to use it.

    Any help is greatly appreciated.

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Thanked 4,650 Times in 4,612 Posts
    Actually, it doesn't matter where you put it, for many purposes.

    So maybe you need to provide a *real* example of what doesn't work for you.

    Here's a clue, though:

    If it's a PHP page, bring it up in your browser.

    Then click on the VIEW menu item.

    Then click on the SOURCE or PAGE SOURCE menu item.

    That will show you the HTML (with JS) as the browser sees it.

    If it doesn't look right, fix the PHP until it does.

    If you think it does look right, but it still doesn't work for you, then copy/paste it here.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Thanked 566 Times in 563 Posts
    for convenience, I tend to do it like this:

    <!DOCTYPE html>
        <meta charset="UTF-8">
    <!-- html here-->
    <script type="text/javascript">
    //javascript here
    so that the html elements are defined before the js starts trying to do stuff to them.

    But there are various ways to do it. I guess others have their advantages.

    If you're having trouble with some real code, why not post it?


    Posting Permissions

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