Solved

Basic Javascript Syntax

Posted on 2007-04-05
4
357 Views
Last Modified: 2010-08-05
Lets talk Javascript... I am a MS freak and I dont know anything about Javascript.  But I have to write some code in javascript...my problem is not with concept but with syntax... I will post a few questions here and hopefully you guys can point me in the right direction...

1. In vbscript you say:
If MyVar IS NOT NOTHING
'DO SOMETHING
How do you say in js?

2. In vbscript you say:
for(i=0 to MyArray.Length)
'DO SOMETHING
Next
How do you say in js?

3. In vbscript you say:
for each value in MyValues
MsgBox(value.Property)
next
How do you say in js?

4. When working with ActiveXObject() what needs to be declared and what does not.  If the object returns a different object in vbscript you can just say this...
Set HTTP = CreateObject("MSXML2.XMLHTTP")
Set xmlDOC =CreateObject("MSXML.DOMDocument")
xmlDOC.Async=False
Dim MYURL
HTTP.Open "GET",URL, False
HTTP.Send()
bOK = xmlDOC.load(HTTP.responseXML)
if Not bOK then
      Set GetXMLDOC = nothing
Else
      Set GetXMLDOC = HTTP.responseXML
end If

In the above example GetXMLDOC  will be the return from that method... so if this were a funtion the caller would then be able to make use of that value... hwo to use in javascript though?

Thanks for your help...
0
Comment
Question by:NTGuru705
  • 3
4 Comments
 
LVL 10

Accepted Solution

by:
geodan7 earned 500 total points
ID: 18858398
1. if (x!=null && x!="") {
//Do something
}

2. for (i=0;i<myArray.length;i++) {
//Do Something
}

3. There is no foreach loop in javascript, but they do have a "for ...in" loop, to loop over each value
for (i in myArray) {

}
http://www.devguru.com/technologies/javascript/11477.asp

0
 
LVL 10

Expert Comment

by:geodan7
ID: 18858426
//Creating and setting the instance of appropriate XMLHTTP Request object to a "XmlHttp" variable function CreateXMLHTTP(){      try      {            XMLHTTP = new ActiveXObject("Msxml2.XMLHTTP");      }      catch(e)      {            try            {                  XMLHTTP = new ActiveXObject("Microsoft.XMLHTTP");            }             catch(oc)            {                  XMLHTTP = null;            }      }      //Creating object in Mozilla and Safari       if(!XMLHTTP && typeof XMLHttpRequest != "undefined")       {            XMLHTTP = new XMLHttpRequest();      }}
0
 
LVL 10

Assisted Solution

by:geodan7
geodan7 earned 500 total points
ID: 18858450
Sorry about the formatting on the last post...just copy and paste it out of there. Here's another snippet for creating and loading xml:

function loadXMLDoc(dname)
{
var xmlDoc;
// code for IE
if (window.ActiveXObject)
{
xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
}
// code for Mozilla, Firefox, Opera, etc.
else if (document.implementation && document.implementation.createDocument)
{
xmlDoc=document.implementation.createDocument("","",null);
}
else
{
alert('Your browser cannot handle this script');
}
xmlDoc.async=false;
xmlDoc.load(dname);
return(xmlDoc);
}
0
 
LVL 1

Author Comment

by:NTGuru705
ID: 18858480
Can you help me actually use the above function.. I actually need to make a call to the webservice across the net (http get) and then use the returned data...
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today I would like to talk about localizing (Internationalization) JavaScript applications. Introduction When creating an application that is going to be used by many people around the globe, it is important to remember that not everyone speak…
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question