1

For example a div tag url added.

<div id="uri_1">www.sitename.com</div>
<div id="uri_2">www.sitename.com/?utm_source</div>

How do I know whether a url is plain or there is a "?" behind the domain. I want to make use jQuery / Javascript is also allowed.

Less is more, I need code like this:

<script>
var uri_1 = $('#uri_1').html();
var uri_2 = $('#uri_2').html();

// alert();
// uri_1 do not wear pins "?"
// uri_2 there is a sign "?"
</script>

Sorry if my question is difficult to understand, but I really need your help. Thanks.

dBoys
  • 31
  • 6

2 Answers2

1

$("#uri_1").html().indexOf("?") != -1

EDIT: Basically the OP wants to know if there exists a ? in its HTML, so we just have to get the index of that char. -1 is the value that indexOf() returns if there was no coincidence in the given string. So, if ($("#uri_1").html().indexOf("?") != -1) would tell us that there exists a ? in the string.

RompePC
  • 815
  • 1
  • 8
  • 17
1

url.indexOf("?") will give position of ? in the string. As a default value, it returns -1 if value does not exists. So if .indexOf returns anything other that -1, this means that value exists in your string.

var url = "your url";
if (url.indexOf("?") == -1) {
  alert("? does not exist in this url");
} else {
  alert("? is exist in this url");
}
Rajesh
  • 24,354
  • 5
  • 48
  • 79
Nishant Dixit
  • 5,388
  • 5
  • 17
  • 29