使用Javascript确定URL中的/category/

使用Javascript确定URL中的/category/,javascript,regex,url,indexof,Javascript,Regex,Url,Indexof,我需要(通过Javascript)包含不同的内容,这取决于从url捕获的主要类别 网站的布局如下: 上述不同的主要类别包括: 艺术、新闻、体育、商业和金融 用javascript实现这一点的最佳方法是什么。 我需要的可能如下所示 if (category = Arts) { alert("Arts"); }else if (category = News) { alert("News"); }... 提前谢谢。您可以像这样访问当前url document.locatio

我需要(通过Javascript)包含不同的内容,这取决于从url捕获的主要类别

网站的布局如下:

上述不同的主要类别包括:

艺术、新闻、体育、商业和金融

用javascript实现这一点的最佳方法是什么。 我需要的可能如下所示

if (category = Arts) {
    alert("Arts");
}else if (category = News) {
    alert("News");
}...

提前谢谢。

您可以像这样访问当前url

document.location.href
你可以做一个

if (    document.location.href.indexOf("categoryYouWant")>-1){
     //whatever you want
}
但是你应该做一个正则表达式

category=document.location.href.match(/example\.com\/(\w+)\//i)[1];

您可以像这样访问当前url

document.location.href
你可以做一个

if (    document.location.href.indexOf("categoryYouWant")>-1){
     //whatever you want
}
但是你应该做一个正则表达式

category=document.location.href.match(/example\.com\/(\w+)\//i)[1];

拆分location.href,然后切换相应的变量。例如:

var url = document.location.href,
    split = url.split("/");

/*
  Split will resemble something like this:
  ["http:", "", "example.com", "Category", "Arts", "Other", "Sub", "Categories", ""]

  So, you'll find the bit you're interested in at the 4th element in the array
*/
switch(split[4]){
  case "Arts":
    alert("I do say old chap");
    break;

  case "News":
    alert("Anything interesting on?");
    break;

  default:
    alert("I have no idea what page you're on :O!");
}

拆分location.href,然后切换相应的变量。例如:

var url = document.location.href,
    split = url.split("/");

/*
  Split will resemble something like this:
  ["http:", "", "example.com", "Category", "Arts", "Other", "Sub", "Categories", ""]

  So, you'll find the bit you're interested in at the 4th element in the array
*/
switch(split[4]){
  case "Arts":
    alert("I do say old chap");
    break;

  case "News":
    alert("Anything interesting on?");
    break;

  default:
    alert("I have no idea what page you're on :O!");
}
我举了一个例子:

<!DOCTYPE html> 
    <html>

    <head>
        <meta charset="utf-8"/>
        <meta name="viewport" content="width=device-width, initial-scale=1"/> 

        <script type="text/javascript">
        function determine(url)
        {
           var myArray = url.split('/'); 
           if(myArray[4] == "News")
           alert(myArray[4]);

        }

        </script>
    </head> 

    <body> 

        <div>       
            <a href="" onclick="determine('http://example.com/Category/News/Other/Sub/Categories/')">http://example.com/Category/News/Other/Sub/Categories/</a>       
        </div>

    </body>
    </html>

函数确定(url)
{
var myArray=url.split('/');
if(myArray[4]=“News”)
警报(myArray[4]);
}
萨卢多斯;)

我举了一个例子:

<!DOCTYPE html> 
    <html>

    <head>
        <meta charset="utf-8"/>
        <meta name="viewport" content="width=device-width, initial-scale=1"/> 

        <script type="text/javascript">
        function determine(url)
        {
           var myArray = url.split('/'); 
           if(myArray[4] == "News")
           alert(myArray[4]);

        }

        </script>
    </head> 

    <body> 

        <div>       
            <a href="" onclick="determine('http://example.com/Category/News/Other/Sub/Categories/')">http://example.com/Category/News/Other/Sub/Categories/</a>       
        </div>

    </body>
    </html>

函数确定(url)
{
var myArray=url.split('/');
if(myArray[4]=“News”)
警报(myArray[4]);
}

萨卢多斯;)

谢谢你的回答,这就是我需要的。感谢所有在下面回答的人。大多数答案似乎使用了相同的
url.split
idea。谢谢大家。谢谢你们的回答,这就是我需要的。感谢所有在下面回答的人。大多数答案似乎使用了相同的
url.split
idea。谢谢大家。