-1

the page url is example.com/1/page.html?user=123456

need to check if the current page url contains

example.com/1/page.html

without

?user=123456

because this is dynamic variable

show alert msg hello .. by javascript check current url

 if ( window.location.href = "example.com/1/page.html") {
alert("hello");
}

but not working

another idea !!

4 Answers4

0

Well window.location.href is a string so you could treat it as such and do some manipulation to remove the query part of the URL ( ? bit ) or you could look into a string contains function.

Look into JS indexOf() or split()

MadDokMike
  • 182
  • 5
0

use pathname

if ( window.location.pathname = "/1/page.html") {
alert("hello");
}
pumpkinzzz
  • 2,907
  • 2
  • 18
  • 32
0

I'd do (split the string on ?, should not break if none) :

if (window.location.href.split("?")[0] === "example.com/1/page.html") {
    alert("hello");
}

Comparison is made with == or === not = (assignment)

window.location.href = "example.com/1/page.html" means window.location.href takes the value "example.com/1/page.html" (don't even know if you can write this property)

sodawillow
  • 12,497
  • 4
  • 34
  • 44
0

You can use window.location.pathnameinstead ofwindow.location.href`

That'll give you just the path

if ( window.location.href = "/1/page.html") {
   alert("hello");
}
Santosh Achari
  • 2,936
  • 7
  • 30
  • 52