- This topic has 1 reply, 2 voices, and was last updated 11 years, 1 month ago by support-michael.
-
AuthorPosts
-
BestmooseParticipantHello there,
I don’t really know how to put it, but I am trying to establish a global variable (in JavaScript) within the
phoneui.documentReadyHandler in order to address all kinds of properties of objects.
i. E. changing the text of a textfield, the label of a button etc. without always putting that
weird “#m1-Here_is_the_file_name-” in front of any object Id that I want to address.For instance to be able using document.getElementById(docref + ‘TextArea1’) or $(‘#’ + docref + ‘TextArea1’).val(‘Fill that Text Area’),
especially to build more ‘global’ functions that can be used more easily.Another example:
ChangeButtonLabel(docref + ‘MyOwnButtonId’, ‘New Button Label’) instead of using
ChangeButtonLabel(‘m1-This_is_the_file_name_of_my_mobi_file-MyOwnButtonId’, ‘New Button Label’)And most important, that “docref” variable stops me from having to change all kind of JavaScript source
when I save my .mobi file under a different file name.I managed to get that “global document referrence” that is preceding any object Id with the following code
/ var docref = ''; // building the MobiOne specific referrence for the document in this variable docref = document.URL.split('\\').pop().split('/').pop(); // first getting the document URL (with a lot of unwanted stuff already removed) docref = docref.replace(/\.[^/.]+$/, ""); // then getting rid of anything past the last period and now the funny (or NOT) part: docref = docref.replace("%20", " "); // I tried to get all %20 out in one replace command but failed :-( docref = docref.replace("%20", " "); // so I repeat this line four times, hoping that noone puts more docref = docref.replace("%20", " "); // than 4 consequent spaces in a file name (oh well...) docref = docref.replace("%20", " "); docref = docref.replace(/[^A-Z0-9]+/ig, "_"); // and now replacing any fancy character or series of it // (including the spaces I added before) with one underscore only! docref = 'm1-' + docref + '-'; // and finally adding the m1 in front and the dash at the end (pfffffffft what a mess)
My questions are now:
Is this going to work on all platforms? (It worked well on Windows and on a server using my iPod [with Safari and Opera-Mini])
Is there a sleeker way to get this docref? (Especially that removal of the %20 placeholders for spaces)
the document.title would be, but if it is defined in the MobiOne Design Center (under Webapp Title) it doesn’t work because the reference within the code always uses the (modified) file name.OK, I know there has to be a better way 🙂
Thanks for any answers…
support-michaelKeymaster>OK, I know there has to be a better way 🙂
I’m not asserting this is a better way but it one that I use frequently to get around the id mangling.
Here is how I traditionally approach looking up widgets using just the property id that I set in the Visual Designer.
1. assign all widgets a unique id in the project
2. the code generator always prepends the path of containers to the the widget id to create the widget’s element ID in the DOM. So I use the jquery select to find the id that ends with the widget property id that I assigned.See the jquery doc here.
Example: button widget id = 'foo' $('[id$=foo]') //returns the DOM element that ends with 'foo', my button id
-
AuthorPosts