开发者

Simple Javascript not working in IE, works in Firefox

开发者 https://www.devze.com 2023-02-14 18:42 出处:网络
The following isa simple piece of code to have javascript open up a soundcloud audio player in a pop-up window.It works perfectly in firefox and chrome, but doesn\'t work in IE7; it just shows a blank

The following is a simple piece of code to have javascript open up a soundcloud audio player in a pop-up window. It works perfectly in firefox and chrome, but doesn't work in IE7; it just shows a blank black screen. Does anyone know why?

I get the yellow drop down that says "to help protect.. IE has restricted this webpage from running scripts or ActiveX controls...." Even when I click on it and say allow, the soundcloud player still doesn't appear.

<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>

The code to call this window might be

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>


I figured it out. You need to use the 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();

    }


Following code works fine in 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>
0

精彩评论

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