-4

I want to check if I'm on a specific page and if I'm inside that page and a div exists to do something.

I know how to check if a specific page exists: if('http://'+location.hostname+location.pathname+'/stats')

I know to check if a specific div exists: if($(".div").length) {

but how can I add the second if inside the first if ?

I've tried this but is not working:

if('http://'+location.hostname+location.pathname+'/stats') {
   if($(".div").length) { //do something;}
}

Is this possible with jQuery ?

agis
  • 1,831
  • 10
  • 33
  • 68
  • Load contents of this page via `$.load` (if it's not on the same domain - try JSONP or something else) – u_mulder Jan 31 '14 at 13:35
  • you need to parse that page, the way you are doing you're trying to find ".div" in your current page – G.Mendes Jan 31 '14 at 13:35
  • 3
    That's *not* how you check if a specific page exists. You could use `if("my-great-page-that-is-made-up")` and it would evaluate as `true`. The problem isn't with the syntax if your nested `if` statements, it's that your outer `if` statement doesn't make sense. – user229044 Jan 31 '14 at 13:36
  • Learn about Ajax or iframes... – epascarello Jan 31 '14 at 13:37
  • @meagar, can you please give me an example on how can you do it ? – agis Jan 31 '14 at 13:38
  • I do not understand why you are telling me to use ajax, I just want to check if I'm on specific URL and if I'm on that URL and a div exists to hide it ? Again why do I need to use AJAX for this ? – agis Jan 31 '14 at 13:42
  • and why are you suggesting that this answer already exists on another page because I don't see any similarity with my question ? – agis Jan 31 '14 at 13:44
  • @Alecs If that is the case then you worded the question very badly. You might consider refactoring it to highlight *that* requirement – CodingIntrigue Jan 31 '14 at 13:44
  • @RGraham, I've modified my question, sorry, my bad, I said if a page exists not **if I'm on a specific page** – agis Jan 31 '14 at 13:48

4 Answers4

0

You could use ajax call and in the success call back you could check if that div element exists

jonasnas
  • 3,540
  • 1
  • 23
  • 32
0

You can use the jQuery.load to load your page and get the content of specific element like the following:

 $( "#result" ).load( "ajax/test.html #container" );

Here, jQuery will load the test.html page and internally will parse and then return the content of element which has id container.

You can see the complete reference in jQuery Load documentation

Vinoth
  • 657
  • 6
  • 12
0

You are just testing that a string exists here (which will always evaluate to true), not that the current URL is equal to what you expect.

if(location.pathname === "/stats" && $(".div").length) {
    // Do Something
}

You need to use the equals parameter to compare against pathname as above.

CodingIntrigue
  • 75,930
  • 30
  • 170
  • 176
0

You will not be able to achieve what you want (unless all pages of interest are in the same domain) without some server-side support of any kind. Browsers will prevent you from gathering content that lives in another domain using AJAX because of the Same-origin Policy.

However the solution is quite simple: implement a server-side service that performs that task and consume it from the client application. If you can't, you may use an online service such as YQL instead of implementing your own.

Please also note that if ('http://www.some-url-in-string.com') doesn't do what you expect. A string doesn't inherit any features based on it's content (1. almost true). I could create a string such as 'the-most-important-string-of-all-times' and it won't be treated differently by the JS engine.

1. Empty strings are falsey while all other strings are truthy.

!!''; //false
!!'anything'; //true
plalx
  • 42,889
  • 6
  • 74
  • 90