Did you know there is a type of image that cannot be included in a webpage using the img element? SVG (short for Scalar Vector Graphics, which is an XML-based markup language like XHTML) images work so differently from most image formats that the normal method of including images in a webpage (via the img element) doesn't work. The img element is equally insufficient for Flash files and Java applets. These files need a little extra something.
Enter the object element, a more complex and versatile element designed to handle more complex data. These include:
.mpeg and .mov,.png and .jpg *,and any other media supported by a computer.
Note: *Internet Explorer does not support this method of including images.
Each object requires one object element, plus the option of one or more child parameter elements.
Class Name: object
src attribute of an img element.object element can be used for a lot of different datatypes, this lets the browser know what it's including. Possible values are:
img element.)object element can use image maps, just like an img element.tabindex attribute used in the a element.The object element is one of two elements (the other is script, which I'll get to later) that can be nested both in the head and body elements. Nesting it in the head is not all that common; and its contents may show up on the webpage (there are ways of hiding it, but that's in the section on CSS.
If object is a descendent of the body element, it must be the descendant of a block-level element such as p or div.
Class Name: param
A parameter is to an object what an attribute is to an element: It controls a property of the embedded object. Unlike attributes, however, what is contained in the param element means nothing to the browser; it is interpreted by the embedded object instead.
The param element is an empty element. It has no end tag.
name attribute.param element.The param element does not use any language or core attributes except for id (mentioned above).
Do note that whether or not the contents of the name and value attributes are case-sensitive—in other words, a capital letter is treated as being distinct from a small letter—depends on what you're embedding. It's safest to assume that the values are case-sensitive.
Since objects are such a versatile element, I'll use more than one demonstration.
The first one is simple: I'm embedding a webpage inside another webpage. This particular embed requires no param elements.
What appears inside the border is the embedded webpage, and the result can be seen below:
By the way, make sure the object element has the correct size and width; otherwise its default size could force your viewers to scroll within the object to see its contents.
Embedding an SVG image works practically the same. I'll embed a graphic used in Chapter 2, which was originally created as such a graphic. All I did here was change the data attribute.
And the result is not what we want. The reason is I didn't change the type attribute, and the browser is reading the file incorrectly.
The type attribute for an SVG image is image/svg+xml
, which tells the browser how to read it.
type AttributeAnd there we go.
Now, you might be wondering why the object element isn't considered empty here, as so far it has had no content beyond its attributes.
Well, an empty element isn't just an element that has all its information in it's start tag; it's one that has all possible information in the start tag, and the object element may (and usually does) have content outside the start tag. For example, if a browser does not, for example, support SVG, I can leave a note inside that object warning of that:
What happens is if the browser can't figure out how to handle the object, it will look at the object element's contents to look for something — anything — it can work with. And while Internet Explorer cannot render SVG, it can certainly render text. Meanwhile, a browser that can render SVG will render it, look for any relevant param elements, and move on. Therefore, this will cause no change whatsoever to what browsers that can display the image show, but Internet Explorer, which doesn't support SVG, will show the error text.
By the way, let this be a lesson to you: Just because a web technology is new and shiny doesn't mean you should use it. Not everyone will be able to support it just yet.
Probably the most common use of the object element is to embed Flash on the webpage. This is where things get complicated and you have to ask all browsers to play nice—which can be a trick.
The value for the type attribute is application/x-shockwave-flash
.
It is here that browser quirks really become a pain. For most browsers, the following code is sufficient:
For Internet Explorer, it's not. This is where the param element becomes really important: Internet Explorer needs a movie
parameter to know what movie is playing.
And now we have our flash animation properly embedded (In the actual animation, the circle simply travels from left to right):
For those with notions of mischief, yes, you can set this up so that Firefox, Opera and the like play one movie, and Internet Explorer plays something else—in other words, it's possible to Rickroll IE.
Of course, we can set other parameters as well for Flash animations:
rougher).
value is set to yesor
true.
trueand
false.
Embedded Java programs, video files, and so on get really complicated and are beyond the scope of this book. But suffice to say, the object element will do for nearly any medium you wish to embed.