简单的 javascript:搜索 url 字符串,做一些事情

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7558709/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 00:31:57  来源:igfitidea点击:

simple javascript: search url for string, do something

javascript

提问by steve

I just need a simple function that will search the current url for a string (ie "nature") and then will add a class to an object. I keep finding ways to search the query, but I want to search the entire url. If possible, without jQuery. If not, jQuery will work.

我只需要一个简单的函数,它将在当前 url 中搜索一个字符串(即“自然”),然后将一个类添加到一个对象中。我一直在寻找搜索查询的方法,但我想搜索整个 url。如果可能,不使用 jQuery。如果没有,jQuery 将工作。

回答by James Allardice

You can get the URL with window.location.href, and search it however you like:

您可以使用 获取 URL window.location.href,然后随意搜索:

var location = window.location.href;
if(location.indexOf("whatever") > -1) {
    //Do stuff
}

window.locationreturns a Locationobject, which has a property hrefcontaining the entire URL of the page.

window.location返回一个Location对象,该对象的属性href包含页面的整个 URL。

回答by bfavaretto

The most basic approach is something like this:

最基本的方法是这样的:

window.location.href.indexOf('nature')

That will return -1if the string is not found. Otherwise, it returns the index of the string inside the URL string.

-1如果找不到字符串,它将返回。否则,它返回 URL 字符串中字符串的索引。

回答by Marc B

Using regexes, as an alternative:

使用正则表达式作为替代:

if (window.location.toString().match(/nature/)) {
    yourobj.className = 'newclass';
}

回答by James Johnson

If you're searching for a value in the QueryString, you can try this:

如果要在 QueryString 中搜索值,可以尝试以下操作:

var searchIndex = window.location.search.indexOf("nature");

Otherwise, you can do this:

否则,您可以这样做:

var searchIndex = window.location.href.indexOf("nature");

You can also do this:

你也可以这样做:

var searchIndex = window.location.href.search("/nature/");

To check whether the word was found, you can do this:

要检查是否找到该单词,您可以执行以下操作:

if (searchIndex > -1)
    //logic here

回答by ZenMaster

I think this one should give you a good starting point. JSFiddleis available here.

我认为这个应该给你一个很好的起点。JSFiddle在这里可用。

<div id="test">Some text</div>


.red
{
    background-color: #FF0000;
}


function handler( url, textToMatch, objectToChange, classToAssign )
{
   if (url.search(textToMatch) != -1)
   {
       objectToChange.className = classToAssign;
   }
}

var url = 'http://www.some-place.com&nature'; //window.location.href in your case
var div = document.getElementById('test');

handler(url, 'nature', div, 'red');

回答by David Aguirre

The hash in the URL caused issues for me, so instead I used a "?".

URL 中的散列给我带来了问题,所以我使用了“?”。

As an example lets say I have the URL: http://example.com?someString

例如,假设我有 URL:http: //example.com?someString

First I retrieved the URL:

首先,我检索了 URL:

var activeURL = window.location.href;

Then I take everything from the index of "?" + 1(because I do not care to include "?") onward.

然后我从“?”的索引中取出所有东西 + 1(因为我不在乎包含“?”)。

var activeItem = aactiveURL.substring(activeUrl.indexOf('?') + 1, activeURL.length);

activeItem will be "someString"

activeItem 将是“someString”