My webpage gets the content from XML via an AJAX call. I retrieve a URL from that XML and use it to create an element on the page.
But that URL can look like:
http://something/something.jpg,- or
http://something/something.swf
This means it can be a image file or can be a .swf file, so I am unable to display that image or .swf file in same tag (which is also created dynamically).
I don’t know how to display both image and .swf file in same tag. How can I solve this?
Why don’t you check the URL before creating the tag? You could use a regular expression, like so:
The
iin the regular expression makes it match regardless of case (so it will match “something.SWF” as well) andtoLowerCasemakes sure we can compare it to a lowercase string (e.g.if (type === 'swf') { }).Now you can use the result to create the correct tag, like this:
Please keep in mind that this example is untested. For example, you probably need to set the
heightandwidthattributed on the<embed>tag.NB:
matchreturns an array which contains the entire match, plus any matched groups. In this case, the array will look like this:['jpg', 'jpg']. That’s because the group,(jpg|swf), which is saved as well as the complete match. If you need to prevent this behaviour, you can use a non-capturing group, like this:(?:jpg|swf), so the group won’t be remembered.