简单的Javascript无法在IE中运行,适用于Firefox

时间:2011-03-05 02:09:41

标签: javascript internet-explorer-7

以下是一段简单的代码,让javascript在弹出窗口中打开一个soundcloud音频播放器。它在firefox和chrome中完美运行,但在IE7中不起作用;它只是显示一个空白的黑屏。有谁知道为什么?

我得到的黄色下拉列表示“帮助保护.. IE已限制此网页运行脚本或ActiveX控件....”即使我点击它并说允许,声音云播放器仍然没有出现。

<HTML>
<HEAD>

<script type='text/javascript'>

   function fetchArguments() {
        var arg = window.location.href.split("?")[1].split("&"); // arguments

        var len = arg.length; // length of arguments
        var obj = {}; // object that maps argument id to argument value
        var i; // iterator
        var arr; // array


   for (var i = 0; i < len; i++) {
        arr = arg[i].split("="); // split the argument
        obj[arr[0]] = arr[1]; // e.g. obj["song"] = "3"
    }
    return obj;
}

    function loadTitle() {
        var args = fetchArguments();
        document.title = "Audio: Accidential Seabirds - " + args["name"];
    }

    function loadMusic() {
        var args = fetchArguments();

        var height = "100";
        object = document.createElement("object");
        object.height = height;
        object.width = "100%";

        nameParam = document.createElement("param");
        nameParam.name="movie";
        nameParam.value ="http://player.soundcloud.com/player.swf?url=http%3A%2F%2Fapi.soundcloud.com%2Ftracks%2F" + args["song"];

        scriptParam = document.createElement("param");
        scriptParam.name="allowscriptaccess";
        scriptParam.value="always";

        embedTag = document.createElement("embed");
        embedTag.allowscriptaccess="always";
        embedTag.height= height;
        embedTag.src="http://player.soundcloud.com/player.swf?url=http%3A%2F%2Fapi.soundcloud.com%2Ftracks%2F" + args["song"];
        embedTag.type="application/x-shockwave-flash";
        embedTag.width="100%";


        object.appendChild(nameParam);
        object.appendChild(scriptParam);
        object.appendChild(embedTag);

        document.getElementsByTagName("body")[0].appendChild(object); // we append the iframe to the document's body

        window.innerHeight=100;
        window.innerWidth=600;
        self.focus();

    }
</script>

    <script type='text/javascript'>
        loadTitle();
    </script>

</HEAD>
<BODY bgcolor="#000000" topmargin="0" marginheight="0" leftmargin="0" marginwidth="0">
    <center>
        <script type='text/javascript'>
            loadMusic();
        </script>  
    </center>
</BODY>
</HTML>

调用此窗口的代码可能是

function PopupMusic(song, name) {
    var ptr = window.open("musicplayer.htm?song="+song+"&name='"+name+"'", song, "resizable='false', HEIGHT=90,WIDTH=600");
    if(ptr) ptr.focus();
    return false;
}

<a href="javascript:listen()" onclick="javascript:PopupMusic('7537509', 'the appearance of new animals')">Listen</a>

2 个答案:

答案 0 :(得分:0)

我明白了。您需要使用setAttributeFunction:

    function loadVideo() {
        var args = fetchArguments();
        var videoFrame = document.createElement("iframe");
        videoFrame.setAttribute('id', 'videoFrame');
        videoFrame.setAttribute('title', 'YouTube video player');
        videoFrame.setAttribute('class', 'youtube-player');
        videoFrame.setAttribute('type', 'text/html');
        videoFrame.setAttribute('width', args["width"]);
        videoFrame.setAttribute('height', args["height"]);
        videoFrame.setAttribute('src', 'http://www.youtube.com/embed/' + args["vid"]);
        videoFrame.setAttribute('frameborder', '0');
        videoFrame.allowFullScreen;

        document.getElementsByTagName("body")[0].appendChild(videoFrame); // we append the iframe to the document's body

        self.focus();

    }

答案 1 :(得分:0)

以下代码在IE / FF / Chrome中正常运行:

<script type='text/javascript'>
var musicSrc = 'http://player.soundcloud.com/player.swf?url=http%3A%2F%2Fsoundcloud.com%2Frjchevalier%2Fjust-one-day-ft-deni-hlavinka&amp;color=3b5998&amp;auto_play=true&amp;show_artwork=false';
document.write('<object type="application/x-shockwave-flash" width="100%" height="100%" data="'+musicSrc+'"><param name="movie" value="'+musicSrc+'" /></object>');
</script>