去掉网页中alert和confirm弹出框自带的网址
时间: 2024-03-11 14:47:20 浏览: 33
你可以使用以下 JavaScript 代码,覆盖或重写 `window.alert()` 和 `window.confirm()` 方法,以去掉弹出框中自带的网址:
```javascript
window.alert = function(msg) {
var iframe = document.createElement("iframe");
iframe.style.display = "none";
document.body.appendChild(iframe);
var win = iframe.contentWindow;
win.alert(msg);
document.body.removeChild(iframe);
}
window.confirm = function(msg) {
var iframe = document.createElement("iframe");
iframe.style.display = "none";
document.body.appendChild(iframe);
var win = iframe.contentWindow;
var result = win.confirm(msg);
document.body.removeChild(iframe);
return result;
}
```
这段代码会创建一个隐藏的 `iframe` 元素,并在其中动态创建一个新的 `window` 对象。然后,通过这个新的 `window` 对象来调用真正的 `alert()` 或 `confirm()` 方法,这样就能去掉弹出框中自带的网址。最后,记得将这个 `iframe` 元素从页面中移除,以免影响页面的布局和性能。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)