I'm creating a website which some of it's pages contain at least one media player and because of the page size limitations, I want to load the media player javascript file only in pages that have medias. I store media information in an javascript object in head of the page and other script files are being loaded at the end of body.
I found this solution (using $.getScript
) very nice but I want a solution that doesn't rely on any frameworks. I saw an interesting implementation in jQuery website and I changed it like this:
<script>typeof(player) != 'object' || document.write(unescape('%3Cscript src="/assets/js/player/mediaplayer/jwplayer.js"%3E%3C/script%3E'));</script>
and it works like a charm but as I'm not pro in javascript I want to know how does this work?
- If we don't have a variable with type of object there is no need to check the second condition?
- How other browsers act on this code?
- Do all of them (even older IEs) skip the second condition or they may check all the conditions?
- Is there any better solution with cross-browser behavior?