hi, can anyone help to explain this code.. or is their an equivalent vbscript for this code? please help...don't know about javascript.. thanks in advance..

if(arguments.length == 3){
  var result = "<" + arguments[1] + ">";
  for (var i = 0; i < arguments[2].length; i++){
   result += "<li>" + arguments[2][i] + "</li>";
  }
  result += "</" + arguments[1] + "l>";
  document.getElementById(arguments[0]).innerHTML = result;
 }
}

Recommended Answers

All 2 Replies

if(arguments.length == 3){
  var result = "<" + arguments[1] + ">";
  for (var i = 0; i < arguments[2].length; i++){
   result += "<li>" + arguments[2][i] + "</li>";
  }
  result += "</" + arguments[1] + "l>";
  document.getElementById(arguments[0]).innerHTML = result;
 }
}

It looks like this is creating an unordered list. arguments must be an array that contains an HTML element id name in arguments[0], an HTML tag name in arguments[1], and a list item label in arguments[2].

The part of the code if(arguments.length == 3) says that there should only be 3 elements in the array, arguments[0], [1], and [2].

I'll provide some example definitions for the variables to show you how it works.

var arguments = new Array("menu", "ul");
arguments[2] = new Array("List item 1", "List item 2", "List item 3");

if(arguments.length == 3){
  var result = "<" + arguments[1] + ">";
  for (var i = 0; i < arguments[2].length; i++){
   result += "<li>" + arguments[2][i] + "</li>";
  }
  result += "</" + arguments[1] + ">";
  document.getElementById(arguments[0]).innerHTML = result;
 }
}

will produce:

<ul>
	<li>List item 1</li>
	<li>List item 2</li>
	<li>List item 3</li>
</ul>

and put the result into the element in the HTML labeled "menu".


If you want to see this in action, copy the script on this page and paste it into Tryit editor.

You can also try this code. This demo will automatically inject vbscript in your document, if it detects IE browser's and the code output is also converted into vbscript:
Tested in IE6 -->

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">
<html lang="en">
<head>
<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<meta http-equiv="Content-Style-Type" content="text/css">
<title>Test Page</title>

<style type="text/css">
<!--
html, body {
  background-color : #f0f0f0;
  border : none;
  color : #696969;
  font : normal normal normal 90%/1.6 Verdana, Tahoma, Helvetica, Arial, sans-serif;
  height : auto;
  min-height : 600px;
  min-width : 800px;
  text-align : center;
  vertical-align : baseline;
  width : auto; }
pre {
  border : 1px solid;
  background-color : #F5F5F5;
  font : normal 10pt/1.8 "Lucida Console", "Lucida Sans Typewriter", monospace;
  letter-spacing : 3px;
  padding : 1em 1em 1em 2em;
  }
h2 {
  background-color : #ccc;
  border-bottom : 1px solid #708090;
  border-top : 1px solid #708090;
  font : 700 160% "Bernard MT Condensed";
  line-height : 2ex;
  margin-top : 0;
  min-height : 2ex;
  padding : .100em 0em .100em 1em;
  white-space : nowrap; }

div {
  border : none;
  margin : 0%;
  padding : 0%; }

div#main {
  margin : 0% auto;
  text-align : left;
  height : 100%;
  overflow : hidden;
  width : 98%; }

div.tube {
  border : 1px solid #ccc;
  height : 600px;
  margin : 1% auto 1% auto;
  padding : 1.200em; }
-->
</style>
<script type="text/javascript">
<!--
var vars, vb, div;
var jArray = [ " Red, ", "Black, ", "Blue, ", " and Purple!" ];

jsTOvb = function( args ) {
   div = (( document.getElementById ) ? document.getElementById("output") : document.all.output );
   text = "";
   text += "Dim arg1, arg2, arg3(" + arguments[ 2 ].length + "), count, result\n";
   text += "count = " + arguments.length + "\n";
   text += "arg1 = \"" + arguments[ 0 ] + "\"\n";
   text += "arg2 = \"" + arguments[ 1 ] + "\"\n"; 
   for ( var i = 0; i < arguments[ 2 ].length; i++ ) {
   text += "arg3(" + i + ")" + " = \"" + arguments[ 2 ][ i ] + "\"\n";
   };
   text += "\n   if count == 3 then\n";
   text += "   result = arg1\n";
   text += "   result = result & arg2\n";  
   text += "   For Each item In arg3\n";
   text += "   document.write( result & item )\n";
   text += "   Next\n";
   text += "   Else\n";
   text += "   msgbox \"Undefined JavaScript Arguments!\"\n";
   text += "   End if\n";
   (( document.all && !document.getElementById && !document.layers ) ? document.all.vbs.innerHTML = text : null ); // This will automatically inject vbscript if you are using IE mode Browser's.
   div.innerHTML = '<pre>&lt;script type="text/vbscript"&gt;\n' + text + "&lt;/script&gt;</pre>"; // Javascript convertion: VBScript output
};
   window.onload = function() {
jsTOvb( "My Favorite Colors ", "is ", jArray );
}
// -->
</script>
</head>
<body>
<div id="main">
<div class="tube">
<h2>JavaScript Live DEMO!</h2>
<!--[if IE]>
<script id="vbs" type="text/vbscript">
'Will only work in IE Browser's
</script>
[endif]-->
<div id="output"></div>
</div>
</div>
</body>
</html>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.