Javascript:如何检查 URL 是否包含单词
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34649131/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Javascript: How to check if URL contains a word
提问by Joe SharePoint
I want to check if the URL in a Browser contains the word "Desktop" (I startet the html file from the Desktop). Its url is: "file:///C:/Users/Joe/Desktop/TestAlert.html"
我想检查浏览器中的 URL 是否包含“桌面”一词(我从桌面启动 html 文件)。它的网址是:“file:///C:/Users/Joe/Desktop/TestAlert.html”
But there should appear an alert, but this doesnt works.
但是应该出现警报,但这不起作用。
Here is my code:
这是我的代码:
<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>
I tested this in Firefox, Chrome and Internet Explorer. It would be very nice, if someone can help me!
我在 Firefox、Chrome 和 Internet Explorer 中对此进行了测试。如果有人可以帮助我,那就太好了!
回答by void
The method is String.prototype.indexOf()
if(window.location.href.indexOf("Desktop") > -1) {
alert("Alert: Desktop!");
}
and you don't need DOM Ready for this.
并且您不需要为此准备 DOM。
回答by Naitik Patel
<html>
<head>
<script type="text/javascript">
$(document).ready(function () {
if(window.location.href.indexOf("Desktop") > -1) {
alert("Alert: Desktop!");
}
});
</script>
</head>
<body>
<h1>Test001</h1>
</body>
</html>
Please try this it will give you sollution
请试试这个它会给你解决方案
回答by Vegeta
You can use regexp
您可以使用正则表达式
url = window.location.href;
if( url.match(/desktop/gi) ) {
alert("Alert: Desktop!");
}
Using this you can check two or more words as well like "/desktop|home/gi"
使用它,您可以检查两个或多个单词以及“/desktop|home/gi”
回答by Datsik
Use indexOf
instead
使用indexOf
替代
console.log(window.location.href.indexOf("javascript"));
Same rules apply, anything > -1 means it has found something
相同的规则适用,任何 > -1 表示它找到了一些东西
回答by whistling_marmot
If you want to use jQuery, you'll need to link to its source code.
Also, use indexOf
instead of contains
.
如果你想使用 jQuery,你需要链接到它的源代码。另外,使用indexOf
代替contains
.
<html>
<head>
<script src="http://code.jquery.com/jquery-2.1.4.js"></script>
<script type="text/javascript">
$(document).ready(function () {
if(window.location.href.indexOf("Desktop") > -1) {
alert("Alert: Desktop!");
} else {
alert("window.location.href: " + window.location.href);
}
});
</script>
</head>
<body>
<h1>Test001</h1>
</body>
</html>
Tip: When you have Javascript problems, hit f12 in your browser to open the development tools and then f5 to refresh the page. Then check your console tab for error messages, e.g. 'Uncaught ReferenceError: $ is not defined', and your sources tab for any parts of your code that are underlined in red.
提示:当您遇到Javascript问题时,请在浏览器中按f12打开开发工具,然后按f5刷新页面。然后检查您的控制台选项卡是否有错误消息,例如“Uncaught ReferenceError: $ is not defined”,以及您的代码的任何用红色下划线部分的源选项卡。
回答by alemac852
var url = window.location.href;
console.log(url);
console.log(~url.indexOf("#product-consulation"));
if (~url.indexOf("#product-consulation")) {
console.log('YES');
// $('html, body').animate({
// scrollTop: $('#header').offset().top - 80
// }, 1000);
} else {
console.log('NOPE');
}