15.3. Getting the Current WML
Card's URL: getCurrentCard() Function
The
getCurrentCard() function is used to get the current card's
URL address. The URL returned will be in the relative form if the URL
base of the current WML file and that of the current WMLScript file
are the same, otherwise the URL returned will be in the absolute
form.
If
there is no current card or if there is an error, then
getCurrentCard() returns
invalid.
The
following example illustrates how to use the getCurrentCard()
function. Let's say there is a WML document tutorial_wml_eg.wml
that contains the markup below:
<card
id="card1" title="Card 1"> <p> <a
href="tutorial_wmlscript_eg.wmls#func1()">Run
WMLScript</a> </p> </card>
If
the "Run WMLScript" anchor link is selected, the func1()
function in the tutorial_wmlscript_eg.wmls
file will be executed. Let's say the code of the func1()
function is:
extern
function func1() { var url_str =
WMLBrowser.getCurrentCard(); }
func1
contains one line of code. If it is executed, the url_str
variable will contain the string value "tutorial_wml_eg.wml#card1".
Note that the relative URL returned is relative to the URL base of
the current WMLScript file.
To
transform the relative URL into the absolute form, we need to
know the URL base of the current WMLScript file. The getBase()
function of the URL
standard library can help us achieve this. For example, suppose we
call the getBase()
function in the tutorial_wmlscript_eg.wmls
file and the value returned from it is
"http://www.developershome.com/examples/wmlscript/tutorial_wmlscript_eg.wmls".
Then the absolute URL address of the current WML card is
"http://www.developershome.com/examples/wmlscript/"
+ "tutorial_wml_eg.wml#card1" (the value returned by the
getCurrentCard()
function).
If
the URL of the current WML card contains query parameters (for
example "tutorial_wml_eg.wml?x=abc&y=123#card1"),
they can be extracted using the getQuery()
function of the URL
standard library.
Feedback Form ( ExpandCollapse)
|