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 3 of 3
  1. #1
    New Coder
    Join Date
    Sep 2010
    Posts
    34
    Thanks
    22
    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:
    Code:
    <SCRIPT LANGUAGE="JavaScript">
     // functions and whatnot here  
     function makeSomethingHappen(field, count, max) {
     
     if (something.works > true)
     field.value = i.will.crap(0, max);
     else
     // calculate the remaining characters  
     field.value = max - more.nodoze.caffein;
     }
     </script>
    
      <center>
     <form name=sample action="">
     <input type="text" name="hours">
    onKeyDown="CountLeft(some.js.looking,stuff.here);" 
     onKeyUp="CountLeft(some.more.js.looking,stuff.here);">
    </form>
    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
    Posts
    25,200
    Thanks
    75
    Thanked 4,342 Times in 4,308 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
    Posts
    2,925
    Thanks
    56
    Thanked 549 Times in 546 Posts
    for convenience, I tend to do it like this:

    Code:
    <!DOCTYPE html>
    <html>
      <head>
      <title>blank</title>
        <meta charset="UTF-8">
    </head>
    <body>
    
    <!-- html here-->
    
    <script type="text/javascript">
    
    //javascript here
    
    </script>
    </body>
    </html>
    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
    •