试图获取window.URL在IE8中工作。获得未定义的错误

时间:2015-10-21 05:15:13

标签: javascript html angularjs pdf internet-explorer-8

我在IE8中使用window.URL时遇到问题。请有人帮忙。我试图获得window.URL在IE8中工作。请按照以下代码。

    <!DOCTYPE html>
<html>
    <head>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">

    </head>

    <body>
        <p>This text should be red.</p>
        <script>


            window.Blob = Blob = function (b) {
                return {
                    data: b
                };
            };

            var myBlob;

            window.URL = window.URL || window.webkitURL;

            console.log('URL   '+URL );
            if (window.Blob) {
                console.log('Check URL => '+URL );
                console.log('Check window.URL  =>  '+window.URL );
                console.log('Check window.Blob  =>  '+window.Blob );
                myBlob = new Blob(['body { color: red; }'], {type: 'text/css'});  
                appendLinkElement();
                alert("The Blob() constructor was used.");    
            }else {
                console.log('Check URL => '+URL );
                console.log('Check window.URL  =>  '+window.URL );
                console.log('Check window.Blob  =>  '+window.Blob );
                document.getElementsByTagName('body')[0].innerHTML = "<h3>Blob objects not supported - please upgrade your browser.</h3>";
            }

            function appendLinkElement() {
                var link = document.createElement('link');
                link.rel = 'stylesheet';
                link.href = window.URL.createObjectURL(myBlob);
                document.body.appendChild(link);    
            } 
        </script>
    </body>

</html>

实际问题是在新窗口中将PDF数据(arraybuffer)显示为PDF页面。 PDF不应下载到TEMP文件夹 脚步: 我从RESTful AJAX调用获得了arraybuffer响应。 在JS中将其转换为BLOB。 IE8不支持BLOB,但我在上面添加了blob构造函数。现在我在window.URL中遇到问题,因为我无法使用window.URL.createObjectURL(blob)将BOLB转换为doc URL

请查看以下代码:

searchService.viewDocument(docId)
                        .success(function(data, status, headers, config) {
                        if (window.Blob) {
                            var blob = new Blob([data], {type: "application/pdf"});    //IF BLOB constructor added this will work for IE8

                            console.log("window "+window);
                            console.log("window.URL "+window.URL);
                            console.log("window.webkitURL "+window.webkitURL);
                            console.log("window.mozURL "+window.mozURL);
                            console.log("window.msURL "+window.msURL);

                            var URLLink = window.URL || window.webkitURL || window.mozURL || window.msURL;
                            console.log('URLLink ='+URLLink );      // this is NULL OR UNDEFINED in IE8  Code works in other browsers like Chrome and IE11

                            var fileURL = URLLink.createObjectURL(blob);
                            $scope.content = $sce.trustAsResourceUrl(fileURL);  //AngularJS code

                            var pdfWindow = window.open("", "PDF View", "width=1833, height=1000");
                            var pdfHtmlData = '<head><title>PDF View</title></head><body><div ><object width="1833" height="1000" data="' + $scope.content + '" type="application/pdf"></object></div></body>';
                            pdfWindow.document.write(pdfHtmlData);
                        }else{
                            console.log("viewDocument BLOB not supported.");

                        }

                    }).error......

1 个答案:

答案 0 :(得分:1)

你可以使用window.location,它包含你需要的一些信息。

使用此代码:      window.location.href

你可以在下面看到这个对象的样本

{hash:“”,host:“st.msc.ir”,主机名:“domain.com”,href:“http:// ...”,路径名:“/ login.aspx”,端口: “”,protocol:“http:”,搜索:“?ReturnUrl = ...”}

如果您想在没有任何代码更改的情况下解决问题,请在代码

之前添加此行
window.URL=window.URL?window.URL:window.location.href;