检查字符

时间:2016-09-23 13:30:19

标签: javascript html string

我想知道,我怎样才能看到字符串中特定字符串部分之后是否有任何字符/字符串。我的问题听起来含糊不清,但这是我的真实例子:

我可以有2个网址: http://domain/classAdd?itemId=1123123http://domain/classAdd?ItemId=

我想在HTML中添加关于网址的条件,以便在我的网站上使用特定的标题。 如何检查网址是否有itemId?

目前我只得到:location.indexOf('classAdd?itemId=') == -1我试图验证长度,但不起作用。我搜索了很多,但我没有找到任何回答我的问题,只有如何从字符串中获取特定字符串。我的ID是动态的,子字符串不会起作用

谢谢你们。

2 个答案:

答案 0 :(得分:1)

检查是否

window.location.href.substring(window.location.href.indexOf('classAdd?itemId=') + 16)

为您提供itemId

答案 1 :(得分:0)

function(){
    var indexOfEqual = window.location.href.indexOf("=");
    if(indexOfEqual < window.location.href.length) return true;
    return false;
}