English 中文(简体)
如何检查《URL》是否含有某种描述?
原标题:How to check if the URL contains a given 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>
最佳回答

页: 1

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<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)

这样做。 或者,你可以使用一种规章:

if (/franky/.test(window.location.href))

您将使用。 类似:

if(window.location.href.indexOf("franky") != -1){....}

并通知您将另外添加<代码>href:

if(window.location.toString().indexOf("franky") != -1){....}

https://developer.mozilla.org/en/window.position”rel=“noretinger”>window. place 斜体为<代码>toString()。 因此,你可以这样做:

(  +window.location).includes("franky")

window.location.toString().includes("franky")

http://web.archive.org/web/20120505074801/https://developer.mozilla.org/en/DOM/window.position”rel=“noreferer”>old docs :

Location objects have a toString method returning the current URL. You can also assign a string to window.location. This means that you can work with window.location as if it were a string in most cases. Sometimes, f或example when you need to call a String method on it, you have to explicitly call toString.

同样:

    <script type="text/javascript">
        $(document).ready(function () {
            if(window.location.href.indexOf("cart") > -1) 
            {
                 alert("your url contains the name franky");
            }
        });
    </script>

The regex way:

var matches = !!location.href.match(/franky/); //a boolean value now

或者简单地说,你可以使用:

if (location.href.match(/franky/)) {

我利用这一方法测试网站是否在当地运行或服务器上运行:

location.href.match(/(192.168|localhost).*:1337/)

检查href是否包含192.168 localhost 之后是<代码>:1337。

如你所知,如果情况出现比比照的trick滴,则使用reg比其他解决办法具有优势。

www.un.org/Depts/DGACM/index_french.htm

if(document.URL.indexOf("searchtext") != -1) {
    //found
} else {
    //nope
} 

就此而言,它缩短了时间,并实际操作为window. place.href:

if (document.URL.indexOf("franky") > -1) { ... }

另外,如果你想要检查以前的《欧洲刑法》:

if (document.referrer.indexOf("franky") > -1) { ... }

越狱

<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>

如果你把你的体格转换为低级或高级,则是一种良好做法,因为指数方法敏感。

如果贵国的搜索点是敏感的,那么你就只能使用指数Of()方法,而不会将调子转换为下级或上级:

var string= location.href;
var convertedString= string.toLowerCase();

if(convertedString.indexOf( franky ) != -1)
{
    alert("url has franky");
}
else
{
    alert("url has no franky");
}

相反,我喜欢这种做法。

top.location.pathname.includes( franky )

在许多情况下,它都发挥了作用。

为此:

<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> 

Try indexOf

if (foo.indexOf("franky") >= 0)
{
  ...
}

You can also try search (for regular expressions)

if (foo.search("franky") >= 0)
{
  ...
}

Use Window.location.href to take the url in javascript. it s a property that will tell you the current URL location of the browser. Setting the property to something different will redirect the page.

if (window.location.href.indexOf( franky ) > -1) {
     alert("your url contains the name franky");
}

我愿创建<条码>boolean,然后在符合逻辑的<条码>中加以使用。

//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 );
}

a 窗户所在地是一个包含多种方法的物体,其中一些是与URL有关的载体,因此,你可以安全地搜寻目标明确的护卫:

const href = location.href;
// "https://stackoverflow.com/questions/4597050/how-to-check-if-the-url-contains-a-given-string"

// another option 
const pathname = location.pathname;
// "/questions/4597050/how-to-check-if-the-url-contains-a-given-string"

// search for string safely
pathname.includes("questions"); // true
href.includes("questions"); // true

The Location Object

页: 1

                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 );
                }

由于边界线(<>条码/编码>或类似装置,经常表达对许多人来说更为理想。 <代码>0-9、a-zA-Z_均在下一个配对的该边上,或字母数字特性与线上或线上下或起步相连接。

if (location.href.match(/(?:|_)franky(?:|_)))

如果您使用<代码>if(window. place.href.indexOf(“sam”),则请与<代码>flotsam和same相匹配,除其他外,tom将可与红mato相匹配,明天与无雷克。

使其对个案敏感与删除<代码>i一样简单。

此外,增加其他过滤器同样容易。

if (location.href.match(/(?:|_)(?:franky|bob|billy|john|steve)(?:|_)/i))

请允许我谈谈<代码>(?:<>:>。 RegEx通常将_定义为word nature,因此它不会造成一字边界。 我们利用这个<代码>(?:<>>)来处理这一问题。 看看它是否在座右边发现<代码><>/code>或_

其他语言可能需要使用类似语言。

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

<>支持定期表达的其他语文的记号p{L},但javascript没有,因此探测外国特性的任务会变得更容易。 缩略语

This is my code ♥

function CheckUrl(url) {
var checkA = url.match(/(https|http)://(.*?).(.*?).(.*?)(/|  )/g);
var checkP = url.match(/(https|http)://(.*?).(.*?)(/|  )/g);
if (checkA != null || checkP != null) {
    return true;
}
else {
    console.log("Error", "The link is not valid");
}
return false;}

You can use javascript string method match

const url = window.location.href;
const find =  questions ;
const found = url.match(find);

console.log(url);

if(found !== null && found[0] === find){
  console.log( You are in the questions page );
} else {
  console.log( You are not in the questions page );
}

附录

<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>

url表格是www. localhost.com/web_form_response.html?text_input=stack&over=flow

<代码><p id=“response”>将改为stack





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签