0

I have a simple question. I want do some javascript. When the url is #video. For example. This is my website: www.website.nl. When the url is www.website.nl/#video. Than i must do what javascript. How can i make that:

A start with this code:

$("a[href='http://www.website.nl/#video']"){
            alert("test");
        };
Mike Vierwind
  • 1,482
  • 4
  • 23
  • 44
  • 1
    See this: http://stackoverflow.com/questions/3552944/how-to-get-the-anchor-from-the-url-using-jquery It explains how to get the anchor url. – Tim Nov 24 '12 at 16:43

2 Answers2

0

Try this:

var url = $('a').attr('href');
var hash = url.substring(url.indexOf("#")+1);

if ( hash === 'video' ) {
  alert('test');
}

<a href="http://www.website.nl/#video">a</a>​

http://jsfiddle.net/EGHuB/9/

Barlas Apaydin
  • 7,233
  • 11
  • 55
  • 86
0

You have right selector you, but wrong syntax for block enclosing alert

Live Demo

$("a[href='http://www.website.nl/#video']").each(function(){
            alert("test");
});​

If you know you have only one element then you do not need each

Live Demo

 alert($("a[href='http://www.website.nl/#video']")[0].href);​

or

Live Demo

alert($("a[href='http://www.website.nl/#video']").attr('href'));​
Adil
  • 146,340
  • 25
  • 209
  • 204