LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 06-05-2011, 10:56 AM   #1
TheIndependentAquarius
Senior Member
 
Registered: Dec 2008
Posts: 4,619
Blog Entries: 29

Rep: Reputation: 896Reputation: 896Reputation: 896Reputation: 896Reputation: 896Reputation: 896Reputation: 896
[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!

Last edited by TheIndependentAquarius; 06-05-2011 at 10:57 AM.
 
Old 06-05-2011, 02:32 PM   #2
paulsm4
Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
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
 
1 members found this post helpful.
Old 06-05-2011, 02:42 PM   #3
SigTerm
Member
 
Registered: Dec 2009
Distribution: Slackware 12.2
Posts: 379

Rep: Reputation: 233Reputation: 233Reputation: 233
Quote:
Originally Posted by Anisha Kaul View Post
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()".
 
1 members found this post helpful.
Old 06-05-2011, 04:57 PM   #4
dugan
Senior Member
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 4,566

Rep: Reputation: 1394Reputation: 1394Reputation: 1394Reputation: 1394Reputation: 1394Reputation: 1394Reputation: 1394Reputation: 1394Reputation: 1394Reputation: 1394
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.

Last edited by dugan; 06-05-2011 at 05:19 PM.
 
1 members found this post helpful.
Old 06-05-2011, 08:46 PM   #5
TheIndependentAquarius
Senior Member
 
Registered: Dec 2008
Posts: 4,619
Blog Entries: 29

Original Poster
Rep: Reputation: 896Reputation: 896Reputation: 896Reputation: 896Reputation: 896Reputation: 896Reputation: 896
Quote:
Originally Posted by paulsm4 View Post
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 View Post
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 View Post
You also posted this question to Stackoverflow. I've answered it there.
Good to know that it is you and thanks.
 
Old 06-05-2011, 10:04 PM   #6
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Distribution: UBUNTU 5.10 since Jul-18,2006 on Intel 820 DC
Posts: 607

Rep: Reputation: 127Reputation: 127
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.
 
Old 06-05-2011, 10:53 PM   #7
TheIndependentAquarius
Senior Member
 
Registered: Dec 2008
Posts: 4,619
Blog Entries: 29

Original Poster
Rep: Reputation: 896Reputation: 896Reputation: 896Reputation: 896Reputation: 896Reputation: 896Reputation: 896
Quote:
Originally Posted by SigTerm View Post
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.

Last edited by TheIndependentAquarius; 06-05-2011 at 10:55 PM.
 
Old 06-05-2011, 11:03 PM   #8
TheIndependentAquarius
Senior Member
 
Registered: Dec 2008
Posts: 4,619
Blog Entries: 29

Original Poster
Rep: Reputation: 896Reputation: 896Reputation: 896Reputation: 896Reputation: 896Reputation: 896Reputation: 896
Quote:
Originally Posted by AnanthaP View Post
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.

Last edited by TheIndependentAquarius; 06-05-2011 at 11:19 PM.
 
Old 06-06-2011, 12:48 AM   #9
TheIndependentAquarius
Senior Member
 
Registered: Dec 2008
Posts: 4,619
Blog Entries: 29

Original Poster
Rep: Reputation: 896Reputation: 896Reputation: 896Reputation: 896Reputation: 896Reputation: 896Reputation: 896
Quote:
Originally Posted by dugan View Post
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
 
Old 06-06-2011, 01:15 AM   #10
dugan
Senior Member
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 4,566

Rep: Reputation: 1394Reputation: 1394Reputation: 1394Reputation: 1394Reputation: 1394Reputation: 1394Reputation: 1394Reputation: 1394Reputation: 1394Reputation: 1394
Quote:
Originally Posted by Anisha Kaul View Post
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.

Last edited by dugan; 06-06-2011 at 01:42 AM.
 
1 members found this post helpful.
Old 06-06-2011, 01:38 AM   #11
TheIndependentAquarius
Senior Member
 
Registered: Dec 2008
Posts: 4,619
Blog Entries: 29

Original Poster
Rep: Reputation: 896Reputation: 896Reputation: 896Reputation: 896Reputation: 896Reputation: 896Reputation: 896
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 View Post
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).

Last edited by TheIndependentAquarius; 06-06-2011 at 01:44 AM.
 
Old 06-06-2011, 01:53 AM   #12
TheIndependentAquarius
Senior Member
 
Registered: Dec 2008
Posts: 4,619
Blog Entries: 29

Original Poster
Rep: Reputation: 896Reputation: 896Reputation: 896Reputation: 896Reputation: 896Reputation: 896Reputation: 896
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.
 
Old 06-06-2011, 01:55 AM   #13
dugan
Senior Member
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 4,566

Rep: Reputation: 1394Reputation: 1394Reputation: 1394Reputation: 1394Reputation: 1394Reputation: 1394Reputation: 1394Reputation: 1394Reputation: 1394Reputation: 1394
Have you tried overriding QWebPage::javascriptAlert to have it write to the Linux console (using qDebug), and then calling alert?
 
1 members found this post helpful.
Old 06-06-2011, 01:58 AM   #14
TheIndependentAquarius
Senior Member
 
Registered: Dec 2008
Posts: 4,619
Blog Entries: 29

Original Poster
Rep: Reputation: 896Reputation: 896Reputation: 896Reputation: 896Reputation: 896Reputation: 896Reputation: 896
Quote:
Originally Posted by dugan View Post
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.
 
Old 06-06-2011, 02:02 AM   #15
dugan
Senior Member
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 4,566

Rep: Reputation: 1394Reputation: 1394Reputation: 1394Reputation: 1394Reputation: 1394Reputation: 1394Reputation: 1394Reputation: 1394Reputation: 1394Reputation: 1394
Glad to hear it. I've edited my Stackoverflow reply to make it complete.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to print a message from kernel module to terminal console or tty console yogesh_bansal Linux - Newbie 2 02-27-2009 09:10 AM
communicate a php variable to a javascript function Kanaflloric Programming 4 08-11-2006 12:58 PM
pass javascript variable to php ALInux Programming 6 01-06-2006 06:20 AM
javascript variable in a form rksprst Programming 3 01-05-2005 04:34 AM
my linux console don't print color in c file ngan_yine Programming 6 10-18-2004 03:31 AM


All times are GMT -5. The time now is 09:39 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration