HTML and CSS Reference
In-Depth Information
A better way to do this is through object detection. The revised code can be
seen in Listing 2-2. First, we find out whether the SMS API exists. If it doesn't
exist, we send the user to another page; if it does, then we can send our SMS.
Listing 2-2. JavaScript Code Used for Object Detection
// Check to see whether navigator.mozSms is an object (if it exists)
if (typeof navigator.mozSms === "object"){
// If it does, send a message using the built-in SMS API
navigator.mozSms.send("01234567891", "My Message");
} else {
// If it doesn't, send the user to another location
window.location.href = "sendstandardmessage.html";
}
The method of object detection also allows us to provide fallbacks for browser
specific API's. The Firefox 11 nightlies currently only supports the SMS API, but
there may be other browsers and other devices in the future that may support
the same implementation through different methods or classes.
We can turn this into a feature of our application using a class. We can delegate
the sending of the message within a method as seen in Listing 2-3. This should
in theory allow us to use our own API's to send messages within our application.
When browser vendors add the SMS API to their browser, we only need to add
the method to a single location rather than find and replace it in the entire
application.
Listing 2-3. Using Delegation to Send a Message with Our Own Web Service As a Fallback
var Message = function Message(message, recipient){
this.message = message;
this.recipient = recipient;
this.sendSMS = function sendSMS(recipient){
if(typeof navigator.mozSms === "object"){
// Send SMS using the user's mobile phone
navigator.mozSms.send(this.recipient, this.message);
} else if (typeof navigator.otherSms === "object") {
// Use another browser's SMS implementation
navigator.otherSms.sendMessage(this.message, this.recipient);
 
Search WWH ::




Custom Search