-6

I have been reading to stay away from JavaScript for a lot of things, however I was messing around with it and it doesnt seem to work (unless I am blind and have missed something)

<SCRIPT LANGUAGE="text/javascript">
    function testResults() {
        System.out.println("Got into function");
}
</SCRIPT>

And this is my body:

<form action="" method="GET">
    <input type="button" value="Save" name="saveType" style="margin:10px;" onClick="testResults()"/>
</form>

From what I have seen of javascipt this should be fine... Any suggestions?

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
urema
  • 721
  • 4
  • 11
  • 22
  • 5
    I think you have mistaken Java for JavaScript... as far as I know, `System.out.println()` exists in Java, but not in JavaScript. – rhino May 13 '11 at 13:15
  • 1
    System.out.println()? java?, this is JAVASCRIPT!!!! 300 parody ;) anyways. try `alert('got into function')` instead – netbrain May 13 '11 at 13:15
  • 3
    @user291652 - You should not bash languages going simply by "what you have read". There is a lot of ignorance and obsolete data on the web. Pick up a good book and learn the how's and why's of the js lang. – P.Brian.Mackey May 13 '11 at 13:18
  • onClick="testResults():return false;" to stop it from submitting the form as the event bubbles. Also its a fix for an IE6 bug. – Alistair Laing May 13 '11 at 13:19

5 Answers5

8

Use console.log instead of system.out.println...

Javascript is not Java!

kapa
  • 77,694
  • 21
  • 158
  • 175
Kasturi
  • 3,335
  • 3
  • 28
  • 42
2

A few pointers:

  • it's type="text/javascript"
  • why capital letters?
  • System.out.println is Java

Stay away from JavaScript? Currently it's almost like the opposite, but as almost always it depends on what you have to do :)

Alberto Zaccagni
  • 30,779
  • 11
  • 72
  • 106
0
  • Java => System.out.println or System.out.print
  • Javascript => console.log

:)

kapa
  • 77,694
  • 21
  • 158
  • 175
eouti
  • 5,338
  • 3
  • 34
  • 42
0

Javascript doesn't have a standard output like that. You have a few options:

  • If you have a debugging console running, you can use console.log("log a message")
  • You can output debug information to a HTML element: document.getElementById("myelement").innerHTML = "message here";

There are loads more options - I'd suggest learning Javascript properly. It's not that much like Java.

James
  • 13,092
  • 1
  • 17
  • 19
0

to get it work use this instead:

Look here - jsfiddle

<form action="" method="GET">
    <input type="button" value="Save" name="saveType" style="margin:10px;" onClick="testResults()"/>
</form>

<script type="text/javascript">
    function testResults() {
        alert("Got into function");
}
</script>

it is type="text/javascript and not language="text/language" also in javascript you have to use alert().

Sarah West
  • 2,047
  • 2
  • 20
  • 37
  • Thanks - and thanks for not just jumping to be patronising without understanding the circumstances for the coder.... – urema May 14 '11 at 08:50