What happened to the Messenger Plus! forums on msghelp.net?
Shoutbox » MsgHelp Archive » Messenger Plus! for Live Messenger » Scripting » including files

including files
Author: Message:
Zeelia
New Member
*


Posts: 11
– / Male / Flag
Joined: Dec 2007
O.P. including files
Hi!
I have a JavaScript which includes files just as the PHP function called include:
quote:
include("filename");
so what i need is to get the code configured so that it works with msg-plus-live-script. The errors i get are the bold parts of the script which tells me undefined variable or something like that.

code:
function include( filename ) {
    // http://kevin.vanzonneveld.net
    // +   original by: mdsjack (http://www.mdsjack.bo.it)
    // +   improved by: Legaev Andrey
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Michael White (http://crestidg.com)
    // %        note 1: Force Javascript execution to pause until the file is loaded. Usually causes failure if the file never loads. ( Use sparingly! )
    // *     example 1: include('/pj_test_supportfile_2.js');
    // *     returns 1: 1

    var js = document.createElement('script');
    js.setAttribute('type', 'text/javascript');
    js.setAttribute('src', filename);
    js.setAttribute('defer', 'defer');
    document.getElementsByTagName('HEAD')[0].appendChild(js);

    // save include state for reference by include_once
    var cur_file = {};
    cur_file[window.location.href] = 1;

    if (!window.php_js) window.php_js = {};
    if (!window.php_js.includes) window.php_js.includes = cur_file;
    if (!window.php_js.includes[filename]) {
        window.php_js.includes[filename] = 1;
    } else {
        window.php_js.includes[filename]++;
    }

    return window.php_js.includes[filename];
}


so those ore the errors which i have bumped into until now.
so can someone please translate this so that it works with msgpluslive script or tell me another way of including files?
//Zeelia :D

PS: why i want to include files, i think this is a much more easier way of changing language of the application, so that's why. And i find it much harder to write everything in variables in the same .js file and if(language=="english"){ var ... = "..."; } i think that, that method is harder or maybe more time-taking for each language you'll have on your application.
05-07-2008 11:14 PM
Profile PM Find Quote Report
CookieRevised
Elite Member
*****

Avatar

Posts: 15517
Reputation: 173
– / Male / Flag
Joined: Jul 2003
Status: Away
RE: including files
You don't need to include files. Any JS file you put in the script's directory will be loaded automatically.

So if first.js includes:
code:
var myVar = 'Hello World';
And if another file named another.js includes:
code:
Debug.Trace(myVar);
You will get the proper output: "Hello World"

--------

Then there is also the MsgPlus::LoadScriptFile function which loads an external script file (eg: a file in a subdirectory) directly into your current running script.

See the Plus! Scripting Documentation for more...

--------

But for changing language purposes I really really really suggest you to use variables. Using variables is in fact way more easy.

Also, the include() approach will simply not work for certain things and is in overall way too slugish. Using such an approach means that you actually will end up with a massive amount of files which all contain the very same script (just in another language). While the proper approach (with variables) is way shorter.

Even in other programming languages the include() approach is almost never (mis)used for translating purposes since that isn't what it is meant for. The purpose of include() is to seperate much used declarations and libraries and so that you can easly change that stuff later on without the need to mess in the code files. eg: declaring constants and variables, loading needed libraries (which contain other variables, functions, etc). But the fact that all JScript files in the current script's directory are already automatically loaded makes that you don't need this in Plus! scripting anyways. In PHP only the file you tell the compiler/interpreter to run is executed, other files aren't executed. So that's why it has an include function, so those other files are executed (aka included in the current executing code) too.

The approach of if(language=="english"){ var ... = "..."; } is not the best approach either though. Instead, store your translated lines in a data file and simply read in that data file and use like a kind of ID for each string you want to translate in the JScript code itself. This involves arrays, XML or INI or the registry, etc...

Good examples can be found on the forums and/or in various existing scripts.

--------

PS: seeing your questions lately, I really suggest to forget about PHP and trying to mimic all its functions. First skim (well, not skim, actually read :P) the official Plus! Scripting Documentation and first look at the code of some scripts for how things are done. Learning by example and documentation works wonders in this case. ;)

This post was edited on 05-08-2008 at 12:07 PM by CookieRevised.
.-= A 'frrrrrrrituurrr' for Wacky =-.
05-08-2008 11:52 AM
Profile PM Find Quote Report
Zeelia
New Member
*


Posts: 11
– / Male / Flag
Joined: Dec 2007
O.P. RE: including files
Ok! :D
Thank you for the answer, so what i wanted to do was to load an external file to i will look into the documentation, i've read the documentation various times but i have no idea what e.g "enum" is. I also have no idea how to use the functions declared in the documentation, it doesn't really shows examples which i think would be good :P so thats why i first posted an example of php and then i got that site with various functions written in JS but with same function as PHP.

But i will read the documentation more and try to understand and search more, and if i don't understand something and can't find answers i'll make a new thread.

So thank you :)
//Zeelia
05-08-2008 03:42 PM
Profile PM Find Quote Report
CookieRevised
Elite Member
*****

Avatar

Posts: 15517
Reputation: 173
– / Male / Flag
Joined: Jul 2003
Status: Away
RE: including files
[OFF TOPIC]

Don't use those PHP converted functions you find on that webpage to do script programming for Plus! (or Windows in general) just like that and without the knowledge of what those functions exactly do and for what they are meant.


PHP is meant for web development. JScript (and JavaScript) can also be used for web development, but are also used for Windows command line scripting. Windows scripting is different than scripting for the web since the script is not based upon a html page. So you don't have the same objects at your disposal. For example, in a web script you would output your data to html, obviously in command line scripting there is no html/web page to output to. So stuff like window.location.href don't even exist in command line scripting (there is no browser window and thus no URL location).

Furthermore, the functions you'll find on that website are converted to JavaScript. This is not entirly the same as JScript. Plus! scripting uses JScript, not JavaScript and thus some things (although not that much) are different.


---------------------------

If, in the Plus! scripting documentation, it is stated that function X::Y does something, then Y is a property or a method of the object X.

So you call that function like X.Y

And if there is another function which states Y::Z, then you call that function like X.Y.Z.

What the type of the return value of that function is, is stated in the 'syntax' between square brackets in front of the function name. The same with parameters for that function.

This is a general way in describing functions in programming language documentation so that everybody, no matter what language they come from, can understand it without confusion.

It's true that some descriptions can use some examples, but for things like the MsgPlus::LoadScriptFile function an example wouldn't show anything new or more than what is already said in the description. It's just a function with 1 parameter.

eg:

MsgPlus::LoadScriptFile
[boolean] LoadScriptFile(
    [string] ScriptFile
);

Means you call it like this:
    var retValue = MsgPlus.LoadScriptFile("subdirectory\\scriptfile.js");
or simply:
    MsgPlus.LoadScriptFile("subdirectory\\scriptfile");
    if you don't need to know the return value.

"subdirectory\\scriptfile.js" is obviously a string (see [string] in syntax)
reValue is a boolean (see [boolean] in syntax) and thus will be either True or False according to the success of the function.


---------------------------

An enumeration ([enum]) is just a collection of constants. It means that the parameter in question can only contain predefined values. These values make up the enumeration.

eg:

OnEvent_MenuClicked
OnEvent_MenuClicked(
    [string] MenuItemId,
    [enum] Location,
    [object] OriginWnd
);

From this you know that the function is an event. Events are functions which are automatically called when a certain thing happens. The parameters are filled in by Plus! and thus returned to your function. So this works in the opposite way as with functions like MsgPlus::LoadScriptFile where you need to fill in the parameters to send to the function.

The second parameter named 'Location' is an enumeration. Which means it will only return certain values (for what values, see the documentation). In the newer versions of Plus! you can also use the constant names of these variables instead of the numerical values themselfs to check on:

OnEvent_MenuClicked (sMenuItemId, eLocation, oOriginWnd) {
    Debug.Trace("Menu with ID: " + sMenuItemId + " has been clicked");
    // using the name of the constant:
    If (eLocation === MENULOC_CONTACTLIST) {
        Debug.Trace("This happened in the contactlist");
    }
    // or, using the numerical value:
    If (eLocation === 2) {
        Debug.Trace("This happened in a chat window");
    }
}



[/OFF TOPIC]

This post was edited on 05-08-2008 at 04:51 PM by CookieRevised.
.-= A 'frrrrrrrituurrr' for Wacky =-.
05-08-2008 04:37 PM
Profile PM Find Quote Report
Zeelia
New Member
*


Posts: 11
– / Male / Flag
Joined: Dec 2007
O.P. RE: including files
Wow, thank you really much that did help me alot and it did tell me what enum is, in an easy way with examples.

Now that i've downloaded the new documentation, i see that Patchou have added examples which i think will make it much more easier, so thank you for your tips and everything :D

Now one last thing, how would i make an .ini file for example, one .ini file in the language "english" with all the variables that will be used on the .js file and another .ini file in the language "swedish".
How can i make it load the english ini file if the language variable has the string "english" and the same for swedish?

so for example:
if(language=="english"){
load("english.ini");
} else if(language=="swedish"){
load("swedish.ini");
} else {
load("english.ini");
}

so if language is empty or doesn't have either english or swedish load the default, english.
05-08-2008 05:30 PM
Profile PM Find Quote Report
« Next Oldest Return to Top Next Newest »


Threaded Mode | Linear Mode
View a Printable Version
Send this Thread to a Friend
Subscribe | Add to Favorites
Rate This Thread:

Forum Jump:

Forum Rules:
You cannot post new threads
You cannot post replies
You cannot post attachments
You can edit your posts
HTML is Off
myCode is On
Smilies are On
[img] Code is On