Variable isn't defined - Printable Version
-Shoutbox (https://shoutbox.menthix.net)
+-- Forum: MsgHelp Archive (/forumdisplay.php?fid=58)
+--- Forum: Messenger Plus! for Live Messenger (/forumdisplay.php?fid=4)
+---- Forum: Scripting (/forumdisplay.php?fid=39)
+----- Thread: Variable isn't defined (/showthread.php?tid=78655)
Variable isn't defined by SnuZZer on 11-02-2007 at 07:40 PM
Hi.
I'm still a bit new at coding MSG Plus! scripts. I have made this script:
code: function OnEvent_Initialize(MessengerStart)
{
StartDownload();
}
function StartDownload()
{
var HTTP = new ActiveXObject("Microsoft.XMLHTTP");
var XML = new ActiveXObject( "Microsoft.XMLDOM" );
HTTP.onreadystatechange = function()
{
if((HTTP.readyState == 4) && (HTTP.status == 200))
{
var Feed = HTTP.responseText;
XML.loadXML(Feed);
var Image = XML.getElementsByTagName("title")[1].text;
var Width = XML.getElementsByTagName("title")[2].text;
var Height = XML.getElementsByTagName("title")[3].text;
MsgPlus.DownloadFile(Image);
}
}
HTTP.open("POST", "http://www.snuzzer.dk/msgplus/php/explosmnet.php", false);
HTTP.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
HTTP.send("");
}
function OnEvent_DownloadFileComplete(URL, OutFile, Success)
{
if(Success)
{
ComicWnd = MsgPlus.CreateWnd("comic.xml", "TodaysComic");
ComicWnd.ImageElmt_SetImageFile("DisplayComic", "\\" + OutFile);
ComicWnd = Interop.Call("user32.dll", "SetWindowPos", ComicWnd.Handle, 0, 0, 0, Width, Height, 18);
}
}
My problem is, that the debugger says, that Width and Height isn't defined. Can someone please tell me what it gives me that error?
Thanks in advance.
Simon
RE: Variable isn't defined by Matti on 11-02-2007 at 08:17 PM
That's because they're defined in the function "StartDownload", and thus they're private for the function. They aren't accessible outside that function.
To fix this, you have to define Width and Height globally. At the very beginning of your script, outside any function, add this:
code: var Width;
var Height
//Here the rest of the script, in your case: function OnEvent_Initialize(MessengerStart) etc.
and remove the "var" before Width and Height in your function. This way, the variables are accessible by all parts of your script, and can also be changed by any function.
RE: Variable isn't defined by SnuZZer on 11-02-2007 at 08:43 PM
It sounds right, but it doesn't work.
code: var Image;
var Width;
var Height;
function OnEvent_Initialize(MessengerStart)
{
StartDownload();
}
function StartDownload()
{
var HTTP = new ActiveXObject("Microsoft.XMLHTTP");
var XML = new ActiveXObject( "Microsoft.XMLDOM" );
HTTP.onreadystatechange = function()
{
if((HTTP.readyState == 4) && (HTTP.status == 200))
{
var Feed = HTTP.responseText;
XML.loadXML(Feed);
Image = XML.getElementsByTagName("title")[1].text;
Width = XML.getElementsByTagName("title")[2].text;
Height = XML.getElementsByTagName("title")[3].text;
MsgPlus.DownloadFile(Image);
}
}
HTTP.open("POST", "http://www.snuzzer.dk/msgplus/php/explosmnet.php", false);
HTTP.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
HTTP.send("");
}
function OnEvent_DownloadFileComplete(URL, OutFile, Success)
{
if(Success)
{
ComicWnd = MsgPlus.CreateWnd("comic.xml", "TodaysComic");
ComicWnd.ImageElmt_SetImageFile("DisplayComic", "\\" + OutFile);
ComicWnd = Interop.Call("user32.dll", "SetWindowPos", ComicWnd.Handle, 0, 0, 0, Width, Height, 18);
}
}
RE: Variable isn't defined by Matti on 11-03-2007 at 09:55 AM
What error does it give you? Still a "variable not defined" or another error?
You may want to try this:
code: Width = 1 * XML.getElementsByTagName("title")[2].text;
Height = 1 * XML.getElementsByTagName("title")[3].text;
Width and Height will be set to a string value, because the "text" property is a string. Thus, when you pass a string through Interop.Call, it'll create a DataBloc with the Unicode string in and send the pointer instead, which is not what we want. Therefore, we force the string to be converted to a number by multiplying it with 1, and so Width and Height will be numbers and their value should be passed correctly through Interop.Call.
RE: Variable isn't defined by SnuZZer on 11-03-2007 at 03:43 PM
Perfect! It works when I multply the string with one! Many thanks!
RE: Variable isn't defined by roflmao456 on 11-03-2007 at 06:42 PM
@Mattike: can't you use parseInt too?
RE: Variable isn't defined by Felu on 11-04-2007 at 03:57 AM
quote: Originally posted by roflmao456
@Mattike: can't you use parseInt too?
Sure, you can.
RE: Variable isn't defined by Matti on 11-04-2007 at 10:28 AM
But parseInt isn't the best solution for this. parseInt gets the first integer from a string, and that's not always what you need. Here are some proofs:
code: parseInt("85") -> 85 //parseInt finds the integer 85.
parseInt("85.999") -> 85 //parseInt searches until a non-decimal or period is found, and thus only finds 85.
parseInt(" 85.999") -> 85 //parseInt searches from the first non-whitespace character, and finds 85.
parseInt("a85.999") -> NaN //parseInt returns NaN if the first character is not a decimal or a white-space character.
Thus, if you have decimals in your "textual number", parseInt will discard them. However, parseInt is a good choice to convert from numeric systems to the decimal system. Like:
code: parseInt("0xFF", 16) -> 255 //From hexadecimal to decimal
parseInt("100", 8) -> 64 //From octal to decimal
parseInt("1010", 2) -> 10 //From binary to decimal
Of course, in this case you are allowed to choose, since you expect that the XML contains only integers. But personally, I rely more on the type conversions of JScript than on parseInt, especially because parseInt does more than you actually need.
|