Hướng dẫn javascript print variable

What is the best way to echo the variable get_parestotal?

Nội dung chính

  • JavaScript Display Possibilities
  • Using innerHTML
  • Using document.write()
  • Using window.alert()
  • Using console.log()
  • JavaScript Print
  • How do you echo in JavaScript?
  • How do I display a variable in JavaScript?
  • How do you pass data from JS to HTML?
  • How do I pass variables and data from JavaScript to PHP?

Nội dung chính

  • JavaScript Display Possibilities
  • Using innerHTML
  • Using document.write()
  • Using window.alert()
  • Using console.log()
  • JavaScript Print
  • How do you echo in JavaScript?
  • How do I display a variable in JavaScript?
  • How do you pass data from JS to HTML?
  • How do I pass variables and data from JavaScript to PHP?

Some help please! Thank you

  




CMS

6943 gold badges9 silver badges31 bronze badges

asked Sep 22, 2014 at 12:41

7

You can log variables and so on in the console.

e.g.:

console.log(get_parestotal);

You can even concatenate witht he use of + console.log('This is your variable'+get_parestotal)

You can even add styling to your log (color, background-color,...):

console.log('%cThis is your variable'+get_parestotal+'!','color:green;');

There are some alternatives to console.log() you could use:

 console.warn();
 console.info();
 console.error();
 console.debug();

answered Sep 22, 2014 at 12:54

Hướng dẫn javascript print variable

MHXMHX

1,5332 gold badges22 silver badges30 bronze badges

2

You can either do alert(myvar) or console.log(myvar).

Caveat: console.log is only supported in certain browsers...see the link.

answered Sep 22, 2014 at 13:00

CodexCodex

273 bronze badges

if you want a console output than use console.log(get_parestotal);

answered Sep 22, 2014 at 13:06

CMSCMS

6943 gold badges9 silver badges31 bronze badges


JavaScript Display Possibilities

JavaScript can "display" data in different ways:

  • Writing into an HTML element, using innerHTML.
  • Writing into the HTML output using document.write().
  • Writing into an alert box, using window.alert().
  • Writing into the browser console, using console.log().

Using innerHTML

To access an HTML element, JavaScript can use the document.getElementById(id) method.

The id attribute defines the HTML element. The innerHTML property defines the HTML content:

Example



My First Web Page


My First Paragraph


Try it Yourself »

Changing the innerHTML property of an HTML element is a common way to display data in HTML.


Using document.write()

For testing purposes, it is convenient to use document.write():

Example



My First Web Page


My first paragraph.


Try it Yourself »

Using document.write() after an HTML document is loaded, will delete all existing HTML:

Example



My First Web Page


My first paragraph.


Try it Yourself »

The document.write() method should only be used for testing.



Using window.alert()

You can use an alert box to display data:

Example



My First Web Page


My first paragraph.


Try it Yourself »

You can skip the window keyword.

In JavaScript, the window object is the global scope object, that means that variables, properties, and methods by default belong to the window object. This also means that specifying the window keyword is optional:

Example



My First Web Page


My first paragraph.


Try it Yourself »


Using console.log()

For debugging purposes, you can call the console.log() method in the browser to display data.

You will learn more about debugging in a later chapter.

Example




Try it Yourself »


JavaScript Print

JavaScript does not have any print object or print methods.

You cannot access output devices from JavaScript.

The only exception is that you can call the window.print() method in the browser to print the content of the current window.

Example




Try it Yourself »

How do you echo in JavaScript?

Although JavaScript doesn't have an echo statement, you can use the document. write() and the Element. appendChild() methods to achieve the same result.

How do I display a variable in JavaScript?

There are three ways to display JavaScript variable values in HTML pages:.

Display the variable using document. write() method..

Display the variable to an HTML element content using innerHTML property..

Display the variable using the window. alert() method..

How do you pass data from JS to HTML?

JavaScript can "display" data in different ways:.

Writing into an HTML element, using innerHTML ..

Writing into the HTML output using document.write() ..

Writing into an alert box, using window.alert() ..

Writing into the browser console, using console.log() ..

How do I pass variables and data from JavaScript to PHP?

The way to pass a JavaScript variable to PHP is through a request. This type of URL is only visible if we use the GET action, the POST action hides the information in the URL. Server Side(PHP): On the server side PHP page, we request for the data submitted by the form and display the result. $result = $_GET [ 'data' ];