Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect a specific page load with javascript?

I have a gallery page with a table that has an id. Each table cell has an img element. How do I test if the current page has a table and those img elements?

I tried:

if ($("#gallery-table img"))
{
alert("Gallery page");
}

but that code runs on every page even if the elements do not exist.


1 Answers

Try

if ($("#gallery-table img").length)
{
   alert("Gallery page");
}

jQuery is returning an object no matter what, so you'll need to see if anything exists in that object.

like image 180
Xyan Ewing Avatar answered Jul 23 '26 19:07

Xyan Ewing



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!