1 Followers
24 Following
codeing

codeing

Mirroring Your Site Without Duplicating Content

One of the surest ways to lose customers is to make a mobile site that
doesn’t stay up-to-date with the latest information. Throughout this
book, we’ve built a design in straight HTML without any consideration for any server-side technologies; however, I am working under the
assumption that you’re a developer, and you probably have some idea
of how to go about building dynamic websites.

With that in mind, let’s set up a new domain for our site, point it at our
main domain, and then use some server-side logic to detect which URL
the user requested. In this book, I’ll walk you through how to use PHP
to transform our static pages for use on mobile devices. Depending on
your site design, you might be able to swap out templates based on the
host name.

This book doesn’t really cover system administration, so how you set
up your site to respond to multiple domains depends largely on your
web host. You could create a new record in your DNS that points the
domain to the same IP address as your primary address:
www.yourfoodbox.com A 12.34.56.78
m.yourfoodbox.com A 12.34.56.78

Some web hosts, including Dreamhost,2 provide an easy way to mirror
domains. Consult your web host, your sysadmin, or your web server’s
documentation on how you might mirror your domain name. Essentially, all you need to do is make two domains point to the same server.
If all you’re doing is testing on your local machine, you could modify
your local hosts file like this:

127.0.0.1 localhost www.yourfoodbox.dev m.yourfoodbox.dev
You can find this file at c:windowssystem32driversetchosts on Windows; on most Linux and OS X systems, you can find it at /etc/hosts.
You need to have administrative privileges to make changes to this file.

Transforming Content

We can choose from a few approaches to reformat our content for
mobile devices, but these approaches start to fall apart when we look at
them closely. We can’t just use a handheld style sheet because most of
the browsers out there will try to read the screen style sheets anyway,
which eliminates that option. We already discussed the facts that the
mobile users are much more focused on content, and they often have
slow connections (at least in the United States), so we need to minimize
the amount of data we send to them, which in turn will enable them to
load our site more quickly. We need a way to strip out all the cruft and
show a basic page.

If we disable style sheets altogether, our site is surprisingly usable on a
mobile phone. We could also disable the images and replace any images
that have links with regular links. It turns out that all the way back in
2005 a developer named Mike Davidson came up with an extremely
clever solution that will meet our needs perfectly.

Mike’s solution relies on a mechanism that uses PHP to preprocess all
HTML pages and strip out content, but only when the mobile domain
is used. With some slight modifications, his technique gives us exactly
what we need to make a quick mobile version of our site. To do this, you
need to use Apache and have it configured to serve PHP pages. From
this point on, I’ll assume you have that working.

To use this method, we have to ensure that the web server is using the
Apache PHP module to process PHP pages; we also have to ensure that
PHP is properly enabled. This method won’t work if PHP is running as
a regular CGI program. You can test your setup if you create a file in
your web space called info.php and place this content in the file:

Differences between JavaScript and Java

While programming is covered in the programming module of this course, JavaScript differs from Java in some important areas that we will quickly review. JavaScript objects are covered in more detail in later chapters, so we will not go into any great depth here.
In Java, all functions must belong to a class, and for this reason, are called methods. In JavaScript, a function does not have to belong to a particular object at all. When a function does, however, it is often called a method. Functions and methods are both implemented in the same way, using the function keyword. All methods are functions, but not all functions are methods.

Unlike Java, JavaScript does not contain the idea of classes. Instead, JavaScript has constructors, which are a special kind of function that directly creates objects. These constructor functions define the state variables which each object holds and initialises their values. These variables are often called the object's properties. Constructor functions also supply objects with their methods.

In JavaScript, functions are themselves a special kind of object called Function Objects. Function Objects can be called just like normal functions in other languages, but because they are objects they can themselves be stored in variables and can be easily passed around as, say, arguments to other functions. They can also contain properties of their own. Constructor functions have a special Prototype property which is used to implement inheritance, as will be explained later in the chapter on objects. Constructor functions are called using the new keyword when creating objects.

JavaScript communicates with its environment by calling methods on objects representing components of that environment, such as an object representing the window the HTML document is displayed in, an object representing the document itself, and so on. Ignoring the server-side at present, we conceptually have a browser that interacts with a window, which interacts with a document, which is itself the user interface. JavaScript allows the user interface to be programmed. This is accomplished by providing the means, in JavaScript, for a user to interact with a system via the document rendered in the browser window, as depicted below.