简体   繁体   中英

display javascript code in a html page

On IE Quirks mode , I want to use following code to display some javascript functions on a html page:

document.write('<pre>' + someFunction + '</pre>');

But if a function's source contains '<', only source code ahead of '<' will be displayed.

for example :

function a(j){
   var i = 0;
   if(i<j){
       alert(j + ' is greater than zero');
   }
}

will be displayed as :

function a(j){
       var i = 0;
       if(i

how to make it display the full source code of a function ?

instead of using that '<' or '>' symbols you can use their html rxpressions.

Eg &lt; instead of < and &gt; instead of > .

This will solve your problem . Try this.

Replace < symbol by &lt; and > by &gt; .

Use .replace(), eg

function foo(i) {
  if (i < 10) {
    alert("bar");
  }
}
document.write("<pre>"+foo.toString().replace("<","&lt;")+"</pre>");

outputs:

function foo(i) {
    if (i < 10) {
        alert("bar");
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM