2

Сan you explain please.

Why returned, only the first data attribute - "link-1.html", even if I click on the second link

<a class="project-link" href="link-1.html" data-url="link-1.html">
<a class="project-link" href="link-2.html" data-url="link-2.html">

var toUrl = $('.project-link').attr('data-url');
  $('a').click(function() {
  window.location.hash = toUrl;

});

The meaning of such action - my links open through Ajax, but I want to URL displayed in the browser.

I want to make it as behance, if you click on the cards portfolio, they displayed through Ajax in the same window, but it also left open the possibility of direct appeal to the links. That's why I want to URL displayed in the browser address bar

1
  • I am not sure this is correct code both links are missing their </a> attribute hence I think where you problem is coming from. Please confirm your markup is correct. Commented Aug 11, 2015 at 14:39

4 Answers 4

2

You have to get current target url by this

$('a').click(function() {
   var toUrl = $(this).data('url'); // use this as current target
   window.location.hash = toUrl;
});

I recommend you to use .data() when you're retrieving data attributes (only) instead of .attr()

Demo

Sign up to request clarification or add additional context in comments.

3 Comments

Thx! )) And a special thank you PeterKA for the detailed explanation!
One question, how to remove # before a link?
@AntonReshetov I'm not getting your question can you elaborate?
1

.attr( attributeName )
Returns: String
Description: Get the value of an attribute for the first element in the set of matched elements.

$('.project-link') matches more than one element. Therefore, $('.project-link').attr('data-url') will return the value of the data-url attribute for the first element in the set.

To solve this you have maintain the context of the clicked element as you get the attribute, and you do this by using the this keyword.

And if you have other event listeners attached to the element already and you do not want them to fire -- although ajax calls will abort when the user is redirected -- you can use event.stopImmediatePropagation():

$('a').on('click', function( event ) {
    event.stopImmediatePropagation();
    window.location.hash = $(this).data('url'); //this here refers to the element that was clicked.
});

2 Comments

special thank you PeterKA for the detailed explanation
Glad you found this helpful.
0
$('a[data-url]').click(function() {
     window.location.href = $(this).data("url");
});

2 Comments

This code only applies on links with a data-url attribute
Welcome to Stack Overflow! While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value.
0

You might want to try this:

$('.project-link').click(function(){
     window.location.hash = $(this).data('url');
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.