我想检查浏览器中的 URL 是否包含“桌面”一词(我从桌面启动 html 文件)。它的 url 是:“file:///C:/Users/Joe/Desktop/TestAlert.html”
但是应该会出现警报,但这不起作用。
这是我的代码:
<html>
<head>
<script type="text/javascript">
$(document).ready(function () {
if(window.location.href.contains("Desktop") > -1) {
alert("Alert: Desktop!");
}
});
</script>
</head>
<body>
<h1>Test001</h1>
</body>
</html>
我在 Firefox、Chrome 和 Internet Explorer 中对此进行了测试。如果有人可以帮助我,那就太好了!
原文由 Joe SharePoint 发布,翻译遵循 CC BY-SA 4.0 许可协议
该方法是 String.prototype.indexOf()
并且您不需要为此准备好 DOM。