Skip to content Skip to sidebar Skip to footer

How To Display A Javascript Var In Html Body

I am looking for a way to call a javascript number in the body of an html page. This does not have to be long and extravagant just simply work, I just want something like:

Solution 1:

Try This...

<html><head><script>functionmyFunction() {
      var number = "123";
      document.getElementById("myText").innerHTML = number;
    }
  </script></head><bodyonload="myFunction()"><h1>"The value for number is: " <spanid="myText"></span></h1></body></html>

Solution 2:

Use document.write().

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

Solution 3:

<html><head><scripttype="text/javascript">var number = 123;
var string = "abcd";

functiondocWrite(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 can't avoid <script> tag

Solution 4:

You can do the same on document ready event like below

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

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

Solution 5:

<scripttype="text/javascript">functionget_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;
      returnnull;
   };

   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'); //abcvar registryId  = get_param('registryId'); //abcvar registrySectionId = get_param('registrySectionId'); //abcvar apartmentNumber = get_param('apartmentNumber'); //abc</script>

then in the page i call the values like so:

<tdclass="tinfodd"><scripttype="text/javascript">document.write(registrySectionId)
                                                    </script></td>

Post a Comment for "How To Display A Javascript Var In Html Body"