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.

Share Improve this question edited Mar 23, 2013 at 18:33 Matt Coughlin 18.9k3 gold badges47 silver badges59 bronze badges asked Mar 23, 2013 at 18:15 bloomingsmilezbloomingsmilez 4211 gold badge6 silver badges16 bronze badges 0
Add a comment  | 

6 Answers 6

Reset to default 3

Others 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