我正在使用Microsoft Dynamics 365 CRM门户,但是尽管如此,我还是试图在按下按钮时包括一个Sweet Alert 2模式。

它适用于大多数浏览器(Chrome,Firefox和Edge),但适用于Internet Explorer 11。

这是我要部署的整个代码:

<a href="#" id="searchHelp">
    <img border="0" src="http://some.url" alt="?" width="15" height="15">
</a>
<script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script>
<script lang="javascript">
console.log("Javascript loaded");
var helpButton = document.querySelector('#searchHelp');

if(navigator.userAgent.indexOf(".NET") >= 0) {
    console.log('Triggered IE Modal actions');
    helpButton.attachEvent('onclick', function(e) {
        e.returnValue = false;
        loadModal();
    });
} else {
    console.log('Triggered non-IE Modal actions');
    helpButton.addEventListener('click', (e) => {
        e.preventDefault();
        loadModal();
    }, false)
}

function loadModal() {
    var xhr = new XMLHttpRequest();

    xhr.open( "GET", "http://some.url/to-load", false);

    xhr.setRequestHeader("Accept", "text/html");
    xhr.setRequestHeader("Content-Type", "text/html; charset=utf-8");

    xhr.onreadystatechange = function() {
        if(this.readyState == 4) {
            xhr.onreadystatechange = null;
            if(this.status == 200) {
                var parser = new DOMParser(),
                    content = parser.parseFromString(this.response, "text/html"),
                    form = content.querySelector('#liquid_form');

                swal({
                    icon: "info",
                    content: form,
                    button: {
                        text: "Close",
                        value: false,
                        visible: true,
                        className: "",
                        closeModal: true,
                    }
                })
            }
        }
    };
    xhr.send();
}
</script>
<style>
    .swal-modal {
        width: 90% !important;
    }
    .swal-content {
        overflow: hidden;
    }
</style>


该模式在我之前提到的所有浏览器上都很好用,但是单击按钮时不会触发任何操作。它转到页面顶部,并且将#标签添加到URL。消息“ Javascript loading”也未显示。

我试图提高浏览器的兼容性:我已将fetch()更改为XMLHttpRequest,并通过在开发人员工具控制台上进行测试,添加了attachEvent()addEventListener()来添加了浏览器验证。我也尝试在标签上包含type="javascript"。有什么线索吗?

最佳答案

IE11不支持=>箭头功能,即使在当前代码中执行无法达到它们的条件,也无法有条件地使用它们,总的来说,JavaScript是无法通过IE6兼容的浏览器来解析的。

切换到传统的function(e) { ... }语法。

10-05 21:01
查看更多