admin管理员组文章数量:1223713
I wrote the following JavaScript functions :
<script type="text/javascript">
function showVariable(val){
if(typeof(val)!=null && typeof(val)!=false &&typeof(val)!=NaN && typeof(val)!=undefined)
return typeof(val);
else
return val;
}
function print(){
var val = {1, 2, 3};
var res = showVariable(val);
alert(res);
}
</script>
Currently, I can see the result using alert, but I want to know if there is another method to print showVariable
's result in my html document.
I wrote the following JavaScript functions :
<script type="text/javascript">
function showVariable(val){
if(typeof(val)!=null && typeof(val)!=false &&typeof(val)!=NaN && typeof(val)!=undefined)
return typeof(val);
else
return val;
}
function print(){
var val = {1, 2, 3};
var res = showVariable(val);
alert(res);
}
</script>
Currently, I can see the result using alert, but I want to know if there is another method to print showVariable
's result in my html document.
6 Answers
Reset to default 3Others have pointed out how to add text to an existing HTML element. A couple other options are described below.
Error log
For debugging, an alternative to alert()
that's less intrusive is to add the text to the error log. For instance, in Firefox with the Firebug extension:
if (console.log) console.log(res);
Document.write
Another option that probably won't apply to this particular question, but which is sometimes helpful, is to use document.write
. Be careful not to use it after the page has loaded, however, or it will overwrite the page.
For example, the following:
<p>one</p>
<script type="text/javascript">document.write('<p>two</p>');</script>
<p>three</p>
<script type="text/javascript">document.write('<p>four</p>');</script>
Will be displayed in the browser as if the static HTML source code were:
<p>one</p>
<p>two</p>
<p>three</p>
<p>four</p>
typeof
On a side note, the typeof operator returns one of the following string values:
'undefined'
'null' // For browsers that support ECMAScript 6+
'boolean'
'number'
'string'
'function'
'object'
The initial if statement could be refactored as follows:
Instead of this Use this Or this
------------------- ----------------- ------------
typeof(val) != null val !== null
typeof(val) != false val !== false
typeof(val) != NaN typeof val == 'number' !isNaN(val)
typeof(val) != undefined typeof val != 'undefined'
Not sure if you need all of those tests though. It depends on what you're trying to do.
Simply use
function print(){
var val = {1, 2, 3};
var res = showVariable(val);
document.getElementById("myDiv").innerHTML = res;
//if the target is an input :
document.getElementById("myDiv").val = res;
}
You can output to an element:
HTML:
<div id="log"></div>
JavaScript:
function print(value) {
document.getElementById('log').innerHTML += value;
}
Most answers are correct. But for a beginner , it might be confusing . Let me break them into steps:
Problem: Lets say you want to collect the value from a text box and print it into the body of HTML.
- Step 1: Declare anything with an id 'result' (or anything)
- Step 2: Collect from text box into a variable.
- Step 3: Print it using innerHTML
<html>
<body>
Hi everyone
<p id="result"></p>
<textarea cols="40" id="SearchText" rows="1"></textarea>
</div>
<button onclick="myFunction()" type="button">Submit!</button>
</div>
<script>
function myFunction() {
var result = document.getElementById("SearchText").value;
document.getElementById("result").innerHTML = 'hello' + result;
}
</script>
</div>
</body>
<html>
You can set it to the .innerHTML property if an element. Like so:
<div id="output"></div>
document.getElementById('output').innerHTML = val;
You can try document.write();
function print(){
var val = {1, 2, 3};
var res = showVariable(val);
document.write(res);
}
本文标签: Print value returned by a function to screen in javascriptStack Overflow
版权声明:本文标题:Print value returned by a function to screen in javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1739340928a2158929.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论