如何检查URL是否包含给定的string?
我怎么能这样做:
<script type="text/javascript"> $(document).ready(function () { if(window.location.contains("franky")) // This doesn't work, any suggestions? { alert("your url contains the name franky"); } }); </script>  您需要添加href属性并检查indexOf而不是contains 
 <script type="text/javascript"> $(document).ready(function () { if(window.location.href.indexOf("franky") > -1) { alert("your url contains the name franky"); } }); </script> 
 if (window.location.href.indexOf("franky") != -1) 
会做到这一点。 另外,你可以使用正则expression式:
 if (/franky/.test(window.location.href)) 
像这样:
  <script type="text/javascript"> $(document).ready(function () { if(window.location.href.indexOf("cart") > -1) { alert("your url contains the name franky"); } }); </script> 
 你可以像这样使用indexOf : 
 if(window.location.href.indexOf("franky") != -1){....} 
 另外注意为string添加href ,否则你会这样做: 
 if(window.location.toString().indexOf("franky") != -1){....} 
正则expression式的方式:
 var matches = !!location.href.match(/franky/); //a boolean value now 
或者在一个简单的声明中,你可以使用:
 if (location.href.match(/franky/)) { 
我使用它来testing网站是在本地运行还是在服务器上运行:
 location.href.match(/(192.168|localhost).*:1337/) 
 这将检查href是否包含192.168或localhost AND后面跟着:1337 。 
正如你所看到的,当条件有点棘手时,使用正则expression式比其他解决scheme有其优势。
  document.URL应该让你的URL和 
 if(document.URL.indexOf("searchtext") != -1) { //found } else { //nope } 
  window.location不是一个string,但它有一个toString()方法。 所以你可以这样做: 
 (''+window.location).includes("franky") 
要么
 window.location.toString().includes("franky") 
从旧的Mozilla文档 :
位置对象有一个返回当前URL的toString方法。 你也可以给window.location分配一个string。 这意味着在大多数情况下,您可以像使用string一样使用window.location。 有时,例如当你需要调用一个String方法时,你必须显式地调用toString。
 试试这个,它更短,工作正如window.location.href : 
 if (document.URL.indexOf("franky") > -1) { ... } 
另外如果你想检查以前的url:
 if (document.referrer.indexOf("franky") > -1) { ... } 
尝试这个:
 <script type="text/javascript"> $(document).ready ( function () { var regExp = /franky/g; var testString = "something.com/frankyssssddsdfjsdflk?franky";//Inyour case it would be window.location; if(regExp.test(testString)) // This doesn't work, any suggestions. { alert("your url contains the name franky"); } } ); </script> 
尝试indexOf
 if (foo.indexOf("franky") >= 0) { ... } 
您也可以尝试search(对于正则expression式)
 if (foo.search("franky") >= 0) { ... } 
 我喜欢创build一个boolean ,然后在逻辑if使用它。 
 //kick unvalidated users to the login page var onLoginPage = (window.location.href.indexOf("login") > -1); if (!onLoginPage) { console.log('redirected to login page'); window.location = "/login"; } else { console.log('already on the login page'); } 
更容易得到
 <script type="text/javascript"> $(document).ready(function () { var url = window.location.href; if(url.includes('franky')) //includes() method determines whether a string contains specified string. { alert("url contains franky"); } }); </script> 
使用Window.location.href来在JavaScript中的URL。 这是一个属性,会告诉你浏览器的当前URL位置。 将属性设置为不同的将redirect页面。
 if (window.location.href.indexOf('franky') > -1) { alert("your url contains the name franky"); } 
把你的js文件
  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'); } 
假设你有这个脚本
 <div> <p id="response"><p> <script> var query = document.location.href.substring(document.location.href.indexOf("?") + 1); var text_input = query.split("&")[0].split("=")[1]; document.getElementById('response').innerHTML=text_input; </script> </div> 
 urlforms是www.localhost.com/web_form_response.html?text_input=stack&over=flow 
 写入<p id="response">的文本将是stack 
 对于很多人来说,正则expression式会更加优化,因为字边界\b或类似的设备。 当0-9 , az , AZ , _中的任何一个位于下一个匹配的那一侧 ,或者当字母数字字符连接到行或string结束或开始时,将出现字边界。 
 if (location.href.match(/(?:\b|_)franky(?:\b|_))) 
 如果你使用if(window.location.href.indexOf("sam") ,你会得到flotsam和same匹配,换句话说, tom会匹配番茄和明天,没有正则expression式。 
 区分大小写就像删除i一样简单。 
此外,添加其他filter就像
 if (location.href.match(/(?:\b|_)(?:franky|bob|billy|john|steve)(?:\b|_)/i)) 
 我们来谈谈(?:\b|_) 。  RegEx通常将_定义为word character因此不会导致单词边界。 我们用这个(?:\b|_)来处理这个。 查看它是否在string的任一侧上find\b或_ 。 
其他语言可能需要使用类似的东西
 if (location.href.match(/([^\wxxx]|^)(?:franky|bob|billy|john|steve)([^\wxxx]|$)/i)) //where xxx is a character representation (range or literal) of your language's alphanumeric characters. 
所有这一切都比说
 var x = location.href // just used to shorten the code x.indexOf("-sam-") || x.indexOf("-sam.") || x.indexOf(" sam,") || x.indexOf("/sam")... // and other comparisons to see if the url ends with it // more for other filters like frank and billy 
  正则expression式的其他语言的风格支持\p{L}但javascript不会,这将使得检测外部字符的任务变得更容易。  像[^\p{L}](filters|in|any|alphabet)[^\p{L}]