Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a php echo/print equivalent in javascript

Say I want to print html from inside a script tag.

A source like this

<div>foo</div> <script> print('<div>Print this after the script tag</div>'); </script> <div>bar</div> 

should look something like this in browser after the script has run

<div>foo</div> <script> print('<div>Print this after the script tag</div>'); </script> <div>Print this after the script tag</div> <div>bar</div> 

I could write my own code for this purpose but since this looks to me like a very simple problem, I'm guessing either I've missed something or my thinking is flawed in some way and printing is left out intentionally.

Also, somewhat related: I'd like to know if a script is (or can be made) aware of the script tags surrounding it. With this information it would be much easier to find the position for the printed html code to be injected into, assuming it's not highly discouraged.

To clarify: I don't need you to write a print function for me. I only need to know if a native method to achieve this exists and I've missed it or alternatively the reason it shouldn't be done.

EDIT I realized that I didn't think the question through.

I got my facts straight and now almost everything seems to be working. I should've originally mentioned that the print function was needed inside templates - I'm working on a template engine experiment. I managed to work it out by separating scripts from plain html and concatenating the split html sans scripts with script output.

As I was writing the code I noticed that everything wouldn't go so smooth because of the asynchronous nature of js. I guess I was expecting to be able to do any kind of js magic in templates, just like I could in php. Seems like actually supporting async code in a fool-proof manner inside templates will require some more thought.

like image 722
jpeltoniemi Avatar asked Nov 29 '12 19:11

jpeltoniemi


People also ask

What is the JavaScript equivalent of print?

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.

What does echo mean in JavaScript?

It means exactly the same thing as when we echo HTML: that text will be sent to the browser. What the browser does with it is independent of what PHP does.

How do you echo a variable in JavaScript?

The echo keyword is used to display the values of variables var1 and var2 to the HTML document which is readable by the browser normally. This property can be taken into an advantage in writing to JavaScript console using PHP. The JavaScript code can be written in the echo section and taken to the HTML document.

For what echo is used in PHP?

The echo is used to display the output of parameters that are passed to it. It displays the outputs of one or more strings separated by commas. The print accepts one argument at a time & cannot be used as a variable function in PHP.


2 Answers

You need to use document.write()

<div>foo</div> <script> document.write('<div>Print this after the script tag</div>'); </script> <div>bar</div> 

Note that this will only work if you are in the process of writing the document. Once the document has been rendered, calling document.write() will clear the document and start writing a new one. Please refer to other answers provided to this question if this is your use case.

like image 142
Mike Brant Avatar answered Sep 23 '22 21:09

Mike Brant


You can use document.write, however it's not a good practice, it may clear the entire page depends on when it's being executed.

You should use Element.innerHtml like this:

<div>foo</div> <span id="insertHere"></span> <div>bar</div>  <script> document.getElementById('insertHere').innerHTML = '<div>Print this after the script tag</div>'; </script> 
like image 40
Lance Avatar answered Sep 22 '22 21:09

Lance