0

Html:

<div class="example">
<a href="#">Open this link</a>
</div>

I would like to show a div if the user open the link in the same tab.

Jquery:

$(document).ready(function() {
   $('a').click(function() {
      $(this).addClass('someclass');
   });
});

Example: enter image description here

The user click on the link, a div appear showing the "loading", and the div disappear naturally when the other page loads.

But, if the user open the link in a new tab, the div appear, but don't desappear, because the user is not going to the other page.

What can i do?

Iago
  • 1,214
  • 1
  • 10
  • 19
  • Ah, your jQuery is easier to understand than the words. You're looking to highlight the tab that is relative to the current page. This does not require javascript. http://stackoverflow.com/questions/7566238/how-to-make-css-aactive-work-after-the-click – Popnoodles Jun 15 '14 at 02:49
  • Popnoddles, what i'm doing is exactly what i showed. – Iago Jun 15 '14 at 02:52
  • When the user click on a link in the administration panel, a div with the message loading is showed and the div desappear when the user loads the other page. But, if the user use the CTRL+CLICk, for example, the link open in a new tab and the div from the first tab doesn't disappear. – Iago Jun 15 '14 at 02:54
  • I know what you want, the problem is you don't follow what is happening. The new page does not know which link was clicked. The new page DOES know its own URL. Look at the link I gave. – Popnoodles Jun 15 '14 at 02:55
  • See this example, based on a reply. http://jsfiddle.net/don/zD8rR/ I would like to don't show the message Loading if i open the link in a new tab guy... I have no words to clarify anymore. – Iago Jun 15 '14 at 03:04

2 Answers2

0

You can show the div in the click function itself. Something like the following should do what you want

$('nav a').click(function() { // More specific selector to only affect nav links
  $('#divToShow').css({
                 display : "block",
                 opacity : 1
  });
});

Assuming that the div originally has some sort of styles similar to

#divToShow {
    display:none;
    opacity:0;
    -webkit-transition:.3s;
    -moz-transition:.3s;
    -ms-transition:.3s;
    transition:.3s;
    /* Your other styles */
}

If you really want to add a class instead you can do something like this

$('nav a').click(function() { // More specific selector to only affect nav links
  $(document.body).addClass("someclass");
});

with the added CSS of

.someclass #divToShow {
    display:block;
    opacity:1;
}
Zach Saucier
  • 24,871
  • 12
  • 85
  • 147
0

Edit: Read the comments below for the full story.

If I understand you correctly, you'd like something like this:

#loading {
  display: none;
}

<div class="example">
  <a href="#">Link1</a>
  <a href="#">Link2</a>
  <a href="#">Link3</a>
  <a href="#">Link4</a>
</div>

<div id="loading">Loading...</div>

$(document).ready(function() {
  $('a').click(function() {
    $("#loading").show(); // .. or animate() or whatever()
  });
});
LGT
  • 4,957
  • 1
  • 21
  • 22
  • If the user open the link in a new tab, the message loading is showed. The exactly problem i have. http://jsfiddle.net/don/zD8rR/ – Iago Jun 15 '14 at 03:02
  • I'm not sure what you **want** to do. Do you want to force visitors to open links in the current tab or keep displaying a div after a new page has loaded? – LGT Jun 15 '14 at 03:06
  • Show the loading only if the user opened the link of the menu in the same tab. Just it. – Iago Jun 15 '14 at 03:09
  • So.. you want to show a loading div in the **new** tab right before the page is loaded? – LGT Jun 15 '14 at 03:11
  • No, in the same tab. Clicked > A div is showed > The page load (and obviously the div desappear). But if the user open that link in a new tab (use CTRL+CLICK, for example), that div showing the load doesn't appear. – Iago Jun 15 '14 at 03:12
  • That could be difficult since you can check if CTRL is down, but Mac users probably have Command, not to mention people can right-click and select "Open in new tab".. It's probably possible to always show the Loading div on the main page, and if you use a redirect page to open the new address, have that redirect page close your Loading div again. – LGT Jun 15 '14 at 03:17
  • Ok. I'm going to use the `setTimeout` to hide the div automatically after some seconds. I'll mark your reply as the correct. Thank you. – Iago Jun 15 '14 at 03:23