How to read Console strings?

Asked

Viewed 994 times

1

I’m trying to make a code that reads a string from Console browser and assign the string value in id "txtTextoResposta".

To make it more illustrated, I want to make it similar to C#, when we do like this:

string x = Console.ReadLine();

I tried with the ReadLine(), but it didn’t work:

var x = readline();
document.getElementById("txtTextoResposta").innerHTML = x;

I also tried with the Console.Log, but it returns an error:

var x = console.log;
document.getElementById("txtTextoResposta").innerHTML = x;

Error:

Function log() { [Native code] }

2 answers

2


The browser does not allow Javascript code to read the console contents. Probably for security reasons. Short answer, but I see no way around, at least in the browser.

  • What I’m doing is not for a website. It’s kind of a code to paste into the console and run it right there. In this case it wouldn’t work either?

  • @Francisco can put code to run on the page, but you can’t read lines that have been written on the console.

1

You can use the prompt

console.log(prompt('Input'))

Browser other questions tagged

You are not signed in. Login or sign up in order to post.