LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   [Javascript] How to print a variable to Linux console? (http://www.linuxquestions.org/questions/programming-9/%5Bjavascript%5D-how-to-print-a-variable-to-linux-console-884674/)

TheIndependentAquarius 06-05-2011 10:56 AM

[Javascript] How to print a variable to Linux console?
 
Declared as a global variable in an HTML file:
Code:

var pointsArray = new Array();
Inside a Javascript function which IS getting called through Qt's QWebView:
Code:

System.out.write (pointsArray.length);
The above statement doesn't show anything on the console!

paulsm4 06-05-2011 02:32 PM

Hi -

I think I mentioned this in a previous post:

Quote:

Javascript != Java
They are two very, very, VERY different languages.

The most common equivalent of "printf()" in Javascript is "alert()".

'Hope that helps .. PSM

SigTerm 06-05-2011 02:42 PM

Quote:

Originally Posted by Anisha Kaul (Post 4376984)
The above statement doesn't show anything on the console!

A quick google search shows that most likely there is no "system.out" in javascript and the "Traditional" way to print something is either "alert()" or "document.write()".

dugan 06-05-2011 04:57 PM

You also posted this question to Stackoverflow. I've answered it there.

Like other's. I'm curious as to where the System.out.write came from.

TheIndependentAquarius 06-05-2011 08:46 PM

Quote:

Originally Posted by paulsm4 (Post 4377158)
I think I mentioned this in a previous post: They are two very, very, VERY different languages.

Thanks paul, but I know that java and javascript are different languages, but the problem is that I have not studied Java nor I have studied (in detail) Javascript.

I am using Javascript for the Google API, and as we are supposed to search before asking, I looked up Google and found system.out mentioned somewhere w.r.t the same! Had I known Java, I would have known that system.out in specifically in Java!

and all this is not a lie, and at the same time I wonder why no one corrected me on Stackoverflow.

This question was there for past two days! No one answered there so I had to ask here.

Quote:

Originally Posted by SigTerm (Post 4377166)
A quick google search shows that most likely there is no "system.out" in javascript and the "Traditional" way to print something is either "alert()" or "document.write()".

Thanks, I knew about alert, but not about document.write. Will try out today.

Quote:

Originally Posted by dugan (Post 4377239)
You also posted this question to Stackoverflow. I've answered it there.

Good to know that it is you ;) and thanks.

AnanthaP 06-05-2011 10:04 PM

More things about Javascript.

It is also basically type less and only the client (browser) setting controls the fact that the error was ignored.

So during development, you set it the client to display all script errors and abend.

TheIndependentAquarius 06-05-2011 10:53 PM

Quote:

Originally Posted by SigTerm (Post 4377166)
A quick google search shows that most likely there is no "system.out" in javascript and the "Traditional" way to print something is either "alert()" or "document.write()".

Tried "document.write ("11");", it didn't print anything to the console, in fact I think, it tried to print on the widget, though nothing was shown but at least it prevented the map from getting displayed.

I wanted it to display on "console". I am running the qt executable as "./showmap" and then a widget gets displayed on which the map is shown. On a button click, a function gets called and in that function I have written document.write("11"); which doesn't seem to be doing anything normal.

TheIndependentAquarius 06-05-2011 11:03 PM

Quote:

Originally Posted by AnanthaP (Post 4377391)
and only the client (browser) setting controls the fact that the error was ignored.So during development, you set it the client to display all script errors and abend.

Actually I am NOT using any browser. I am displaying the map on the Qt's "widget". There is no website involved. Or if you meant something else then please explain.

TheIndependentAquarius 06-06-2011 12:48 AM

Quote:

Originally Posted by dugan (Post 4377239)

I tried your method, that was indeed helpful in getting the "error messages".

But my question is how should I "print a variable" on the "Linux console" from Javascript functions. I am not talking of error messages here, I just want to see what the variables contain.

Pardon if I have missed some point.

Meanwhile I found this thread: http://stackoverflow.com/questions/1...-error-console

Code:

var d = "Dugan"
throw (d);

This does print Dugan on console but at the same time it refrains the remaining code from getting executed, obviously.

and in the first answer they talk about installing something called firebug, why it is so difficult to print something on the console, I wonder :mad:

dugan 06-06-2011 01:15 AM

Quote:

Originally Posted by Anisha Kaul (Post 4377465)
and in the first answer they talk about installing something called firebug

Yes, something called Firebug. A Mozilla addon used by all web developers. What an unreasonable thing for someone to suggest.

Not an option in your case though, because you're not using Firefox.

Now, if you had made it a functional requirement to write to the Linux console from browser-based Javascript, then you didn't think things through. There is absolutely nothing in either the ECMAScript standard or in any brower's DOM that would make this possible, and browser-based Javascript is specifically designed to run in a sandboxed environment with as few privileges as possible. Exactly why do you think that it would give you access to the operating system's standard output?

In a normal browser, your options are the aforementioned alert and console.log. The second of these, console.log, writes not to standard output ("the Linux console") but to a part of the browser called the Javascript Console. That is what the Stackoverflow thread you linked to was talking about.

Anyway, I would have thought that overriding javascriptConsoleMessage and then using console.log would have worked. But if you already tried it, then I guess not.

TheIndependentAquarius 06-06-2011 01:38 AM

Alright, but there must be some way to see what's in the javascript variables when working with Qt, I hope I am not the first person to be wanting the same! I mean what is the way to debug the logic of the Javascript program when working with Qt?

alert of course works, but I don't consider it nice to go clicking on "ok" buttons every now and then, especially when a loop goes on.

Quote:

Originally Posted by dugan (Post 4377477)
Anyway, I would have thought that overriding javascriptConsoleMessage and then using console.log would have worked. But if you already tried it, then I guess not.

I didn't try console.log after that, actually.

and editing your posts with important infos after long durations is not very helpful since it can be easily missed (no additional mail notification gets sent).

TheIndependentAquarius 06-06-2011 01:53 AM

Now I tried the console.log too as follows, and this worked flawlessly!
Code:

var d = "Dugan";
console.log (d);

Thanks Dugan for being around. Put what you posted here w.r.t console.log on Stackoverflow and I'll select your answer and give you the bounty too. :)

dugan 06-06-2011 01:55 AM

Have you tried overriding QWebPage::javascriptAlert to have it write to the Linux console (using qDebug), and then calling alert?

TheIndependentAquarius 06-06-2011 01:58 AM

Quote:

Originally Posted by dugan (Post 4377491)
Have you tried overriding QWebPage::javascriptAlert to have it write to the Linux console (using qDebug), and then calling alert?

No haven't tried that yet, but your previous post solved my problem, I'll see if later on I get time to try this one too. Thanks again.

dugan 06-06-2011 02:02 AM

Glad to hear it. I've edited my Stackoverflow reply to make it complete.


All times are GMT -5. The time now is 07:51 PM.