How to display javascript var in html body

I am looking for a way to call a javascript number in the body of an html page. It doesn't have to be long and extravagant to just work, I just want something like:

<html> <head> <script type="text/javscript"> var number = 123; </script> </head> <body> <h1>"the value for number is: " + number</h1> </body> </html> 
+21
source share
7 answers

Try it...

 <html> <head> <script> function myFunction() { var number = "123"; document.getElementById("myText").innerHTML = number; } </script> </head> <body onload="myFunction()"> <h1>"the value for number is: " <span id="myText"></span></h1> </body> </html> 
+23
source

Use document.write ().

 <html> <head> <script type="text/javascript"> var number = 123; </script> </head> <body> <h1> the value for number is: <script type="text/javascript"> document.write(number) </script> </h1> </body> </html> 
+21
source

You can do the same on the ready document as shown below

 <script> $(document).ready(function(){ var number = 112; $("yourClass/Element/id...").html(number); // $("yourClass/Element/id...").text(number); }); </script> 

or you can just do it using document.write(number); .

+1
source

 <script type="text/javascript"> function get_param(param) { var search = window.location.search.substring(1); var compareKeyValuePair = function(pair) { var key_value = pair.split('='); var decodedKey = decodeURIComponent(key_value[0]); var decodedValue = decodeURIComponent(key_value[1]); if(decodedKey == param) return decodedValue; return null; }; var comparisonResult = null; if(search.indexOf('&') > -1) { var params = search.split('&'); for(var i = 0; i < params.length; i++) { comparisonResult = compareKeyValuePair(params[i]); if(comparisonResult !== null) { break; } } } else { comparisonResult = compareKeyValuePair(search); } return comparisonResult; } var parcelNumber = get_param('parcelNumber'); //abc var registryId = get_param('registryId'); //abc var registrySectionId = get_param('registrySectionId'); //abc var apartmentNumber = get_param('apartmentNumber'); //abc </script> 

then on the page I call the values ​​like this:

  <td class="tinfodd"> <script type="text/javascript"> document.write(registrySectionId) </script></td> 
+1
source
 <html> <head> <script type="text/javascript"> var number = 123; var string = "abcd"; function docWrite(variable) { document.write(variable); } </script> </head> <body> <h1>the value for number is: <script>docWrite(number)</script></h1> <h2>the text is: <script>docWrite(string)</script> </h2> </body> </html> 

You can shorten document.write but cannot escape <script>

0
source

Index.html:

 <html> <body> Javascript Version: <b id="version"></b> <script src="app.js"></script> </body> </html> 

app.js:

 var ver="1.1"; document.getElementById("version").innerHTML = ver; 
0
source

You cannot add a JavaScript variable to HTML code.

To do this, you need to do the following.

 <html> <head> <script type="text/javscript"> var number = 123; document.addEventListener('DOMContentLoaded', function() { document.getElementByTagName("h1").innerHTML("the value for number is: " + number); }); </script> </head> <body> <h1></h1> </body> </html> 
-2
source

Source: https://habr.com/ru/post/1012713/


All Articles