我在开头说我一般不熟悉 Javascript。我在 HTML 页面中添加了一些简单的内联 JavaScript,以根据是或否弹出窗口来操作 URL。
前提很简单。当页面加载时,会弹出一个窗口(在 onload= 中调用函数),要求用户确认提交。如果他们点击"is",页面将重新加载相同的 URL,但使用不同的 URL 参数。这看起来很奇怪,但我正在使用的应用程序只打印 HTML(新参数将阻止它重新加载确认弹出窗口)。该代码适用于我尝试过的所有浏览器(Chrome、Firefox、Opera、Edge),但不适用于 IE11。确认弹出窗口永远不会出现,它只是正常呈现内容。没有错误。
我已确保该页面允许在 Internet 选项中使用 JavaScript
这是我的网址格式:myurl?conf=false
这是我的代码:
function checkIfConfirmed() {
var url_string = window.location.href;
var url = new URL(url_string);
var isConfirmed = url.searchParams.get("conf");
if (isConfirmed == "false") {
confirmStatus(isConfirmed);
}
}
function setGetParameter(paramName, paramValue) {
var url = window.location.href;
var hash = location.hash;
url = url.replace(hash, '');
if (url.indexOf(paramName + "=") >= 0) {
var prefix = url.substring(0, url.indexOf(paramName + "="));
var suffix = url.substring(url.indexOf(paramName + "="));
suffix = suffix.substring(suffix.indexOf("=") + 1);
suffix = (suffix.indexOf("&") >= 0) ? suffix.substring(suffix.indexOf("&")) : "";
url = prefix + paramName + "=" + paramValue + suffix;
} else {
if (url.indexOf("?") < 0)
url += "?" + paramName + "=" + paramValue;
else
url += "&" + paramName + "=" + paramValue;
}
window.location.href = url + hash;
var xhr = new XMLHttpRequest();
xhr.open('GET', url + hash, true);
xhr.send();
}
function confirmStatus(isUnconfirmed) {
if (confirm("Are you sure you want to proceed?")) {
setGetParameter("conf", "true");
} else {
close();
}
}
<BODY onload="checkIfConfirmed()">
<!-- The content to be displayed -->
</BODY>
最佳答案
经过一番尝试和错误,我发现问题出在使用 URL 对象上。
我的工作 JavaScript:
<script>
function getQueryString() {
var key = false, res = {}, itm = null;
// get the query string without the ?
var qs = location.search.substring(1);
// check for the key as an argument
if (arguments.length > 0 && arguments[0].length > 1)
key = arguments[0];
// make a regex pattern to grab key/value
var pattern = /([^&=]+)=([^&]*)/g;
// loop the items in the query string, either
// find a match to the argument, or build an object
// with key/value pairs
while (itm = pattern.exec(qs)) {
if (key !== false && decodeURIComponent(itm[1]) === key)
return decodeURIComponent(itm[2]);
else if (key === false)
res[decodeURIComponent(itm[1])] = decodeURIComponent(itm[2]);
}
return key === false ? res : null;
}
function setGetParameter(paramName, paramValue)
{
var url = window.location.href;
var hash = location.hash;
url = url.replace(hash, '');
if (url.indexOf(paramName + "=") >= 0)
{
var prefix = url.substring(0, url.indexOf(paramName + "="));
var suffix = url.substring(url.indexOf(paramName + "="));
suffix = suffix.substring(suffix.indexOf("=") + 1);
suffix = (suffix.indexOf("&") >= 0) ? suffix.substring(suffix.indexOf("&")) : "";
url = prefix + paramName + "=" + paramValue + suffix;
}
else
{
if (url.indexOf("?") < 0)
url += "?" + paramName + "=" + paramValue;
else
url += "&" + paramName + "=" + paramValue;
}
window.location.href = url + hash;
var xhr = new XMLHttpRequest();
xhr.open('GET', url + hash, true);
xhr.send();
}
function confirmStatus() {
var confirmed = getQueryString('conf');
if(confirmed = "false") {
if (confirm("Are you sure you want to proceed?")) {
setGetParameter("conf", "true");
} else {
close();
}
}
}
</script>
关于Javascript 代码不适用于 IE11,但适用于所有其他浏览器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58030319/