Problem With JavaScript

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
~ Zymus ~
Forum Newbie
Posts: 11
Joined: Wed Nov 05, 2008 8:36 pm

Problem With JavaScript

Post by ~ Zymus ~ »

Hello,

I am trying to get "test" to print on the screen whenever you click a button. Like so:

Code: Select all

<html>
    <head>
        <title>Test</title>
        
        <script language="javascript">
            <!--
                function Test()
                    {
                        document.write("Test");
                    }
            //-->
        </script>
    </head>
    
    <body>
        <?php
            print('<form method="post">');
            print('<input type ="button" value="Test" onclick="Test()"');
            print('</form>');
        ?>
    </body>
</html>
 
Now, if i were to just replace the body with this:

Code: Select all

<?php
    print('<script language="javascript">Test();</script>');
?>
 
It would write "Test" to the screen like i want it to. But whenever i use the first code, whenever i click the button, the entire page goes white. And nothing shows up. What am i doing wrong?
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Problem With JavaScript

Post by Eran »

You forgot to close the input tag:

Code: Select all

 
print('<input type="button" value="Test" onclick="Test();" />');
 
~ Zymus ~
Forum Newbie
Posts: 11
Joined: Wed Nov 05, 2008 8:36 pm

Re: Problem With JavaScript

Post by ~ Zymus ~ »

Nope, still get the same error:

Code: Select all

<html>
    <head>
        <title>Test</title>
        
        <script language="javascript">
            <!--
                function Test()
                    {
                        document.write("test");
                    }
            //-->
        </script>
    </head>
    
    <body>
        <?php
            print('<form>');
                print('<input type="button" value="Test" onclick="Test()"/>');
            print('</form>');
        ?>
    </body>
</html>
 
Is there anything wrong with the code itself? or could it just be my browser?

Ok, i think im wording it wrong. How would i inject/append text to a page after it's loaded?
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Problem With JavaScript

Post by Eran »

Don't use document.write, use .innerHTML -

Code: Select all

 
<html>
    <head>
        <title>Test</title>
       
        <script language="text/javascript">
            function Test(){
                document.getElementsByTagName('body')[0].innerHTML = 'test';
            }
        </script>
    </head>
   
    <body>
    
        <form method="post">
            <input type ="button" value="Test" onclick="Test();" />
        </form>
        
    </body>
</html>
 
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: Problem With JavaScript

Post by JAB Creations »

Don't use innerHTML either. :P

I took the time to put this together so hopefully you guys can learn from the example I have posted here...
http://www.jabcreations.com/web/javascript/elements.php
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Problem With JavaScript

Post by Eran »

innerHTML is much more performant than DOM methods - http://www.quirksmode.org/dom/innerhtml.html

Also, if you want to use abstaction for DOM methods, use a good library like jQuery instead of rolling your own
Post Reply