trying to get window.URL working in IE8. Getting Undefined Error

拥有回忆 提交于 2020-02-04 08:37:04

问题


I am facing problem in using window.URL in IE8. Could someone please help. I am trying to get window.URL working in IE8. Please following code.

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

Actual problem is displaying PDF data (arraybuffer) on a new window as a PDF page. PDF should not be downloaded to TEMP folder.
steps: I am getting arraybuffer response from a RESTful AJAX call. Converting that to BLOB in JS. BLOB is not supported in IE8 but I added above blob constructor. Now I am facing problem in window.URL as I am not able to convert BOLB to doc URL using window.URL.createObjectURL(blob)

Please take a look in following code:

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:


you can use window.location ,it contains some information that you need.

Use this code: window.location.href

you can see a sample of this object bellow

{hash: "", host: "st.msc.ir", hostname: "domain.com", href: "http://...", pathname: "/login.aspx", port: "", protocol: "http:", search: "?ReturnUrl=..."}

If you want to solve your problem without any code changing add this line before your code

window.URL=window.URL?window.URL:window.location.href;


来源:https://stackoverflow.com/questions/33251443/trying-to-get-window-url-working-in-ie8-getting-undefined-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!