Roman Remus
Roman Remus

Reputation: 11

Jquery: if div exists on a specific* page do

I'm tring to generate a dialog(on all the site's pages) if on one of my site's pages appear a div. More exaclty I have this site, and if you get a friendship request on your user page, I want an allert(or a floating box, I'll see that later) to notify you, wherever you are on the site.

I tried something to use if instruction 2 times:

if('http://'+location.hostname+location.pathname='/profile')

and

if($(".friends .new").length) {
   alert("You have notifications pending");
}

I got no results.

Upvotes: 1

Views: 74

Answers (2)

Randy
Randy

Reputation: 9819

You could use append, and append a piece of JS. append will not fire if the element does not exist.

 $('.friends .new').append('<script>alert("Hello World!");<\/script>');​​​​​​​​​​​​

this will alert hello world if the element .new exists within .friends.

http://jsfiddle.net/dcqxr/14/

Upvotes: 0

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324650

In the first if, you used = when I think you meant +. Your if makes no sense anyway because a non-empty string is always truthy and passes the test.

As for the second, it will work if you already have code creating those elements. If the element is actually class="friends new" then you should remove the space in the selector.

Upvotes: 2

Related Questions