1

For an example, if I were to have a URL https://example.com/search.php?q=foobar&type=0&p=0, and I wanted to target specifically URLs with "type=0" in it, how would I do that?

I know you can use @document url() for URLs who's full name is known, but not with the link above.

EDIT: I'm not looking for the href value, I'm looking for something that will get the current pages URL, and check if it has a particular string in it. For example, if I were at the URL https://example.com/search.php?q=foobar&type=0&p=0, it would change whatever element I tell it to, but if it's https://example.com/search.php?q=foobar&type=1&p=0, it doesn't.

I probably should have made that a bit clearer.

ecjwthx
  • 9
  • 4
  • 1
    @ecjwthx I am not sure that your edit makes it totally clear what you want to do. Do you want to target the element and change its CSS based on "type=0", for example, or do you also want to get the URL and do something with that as well? – PeterJames Sep 13 '22 at 08:02

2 Answers2

1

You can use the an attribute selector for this:

[href*="type=0"] {
  color: green;
}

[href*="zanzabar"] {
  color: red;
}
<a href="https://example.com/search.php?q=foobar&type=0&p=0">Link with type=0</a>
<a href="https://example.com/search.php?q=foobar&p=0">Link without</a>


<h2>Edit searching for just the parameter string without value</h2>
<a href="https://example.com/search.php?q=foobar&zanzabar=0&p=0">Link with type=0</a>
<a href="https://example.com/search.php?q=foobar&zanzabar=1&p=0">Link without</a>
disinfor
  • 10,865
  • 2
  • 33
  • 44
0

Read the url parameters and find the one you are looking for

const urlParams = new URLSearchParams(window.location.search);
const pageType = urlParams.get('type');
console.log(pageType);
epascarello
  • 204,599
  • 20
  • 195
  • 236