开发者

javascript: problem with combining function and if

开发者 https://www.devze.com 2023-03-31 03:50 出处:网络
Ok, I have this code in Javascript: function fullWin() { if (document.getElementById(\'embed\').src = \'vid1.mov\') {

Ok, I have this code in Javascript:

function fullWin() {
    if (document.getElementById('embed').src = 'vid1.mov') {
        window.open('vid1.html');
    }
    else if (document.getElementById('embed').src = 'vid2.mov') {
        window.open('vid2.html');
    }
}

My problem is that when the embed source equal is to vid2.mov, the source changes to vid1.mov and vid1.html opens. I want that if the embed source is equal to vid2.mov, vid2.html opens and viseversa. For those who want to know 开发者_运维问答the html code.

<object height="100%" width="100%">
    <embed id="embed" target="_top" src="Amelie.m4v" autostart="false" height="100%" width="100%" scale="tofit"></embed>
</object>
<div id="div8" onClick="fullWin()">Fullscreen</div>


You need to use two = signs not just one. Or better yet use three ===. More on comparison operators

  • One equals sign is assignment, var a = 1;
  • Two is a type conversion equate, '0' == 0; // true, because '0' is converted to a number
  • Three is strict comparison, '0' === 0; // false, because one is a string and one is a number


if (document.getElementById('embed').src = 'vid1.mov')

This wouldn't work even if properly using a comparison operator (== or ===). The src property of an <emed> element returns a resolved absolute URL such as http://www.example.com/vid1.mov rather than the exact original attribute value.

You can use getAttribute('src') to get the literal attribute value, except that doesn't work on IE; getAttributeNode('src').value is a workaround there, or alternatively try .endsWith('vid1.mov'), or something like:

window.open(document.getElementById('embed').src.replace('.mov', '.html');

I'd suggest using HTML5 video with Flash fallback these days; <embed> is somewhat old-fashioned and problematic.

0

精彩评论

暂无评论...
验证码 换一张
取 消