Solved

Basic Javascript Syntax

Posted on 2007-04-05
4
355 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

When you need to keep track of a simple list of numbers or strings, the Array object is your most direct tool.  As we saw in my earlier EE Article (http://www.experts-exchange.com/A_3488.html), typical array handling might look like this: (CODE) B…
In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
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…

911 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now