admin管理员组文章数量:1026989
I have looked at a lot of examples on SO but can't seems to see what the issue is with my code:
I have urls that have hash tags / jump to id values appended to them. I want to check if the url has a hash tag and use that value as a click event like i would do on the page when clicking a button.
example url:
jquery code:
jQuery(document).ready(function() {
if ( window.location.hash ) {
function showVideo() {
alert('in the function');
}
jQuery(window.location.hash).click(showVideo);
}
});
current click event triggers function
showVideo();
I have looked at a lot of examples on SO but can't seems to see what the issue is with my code:
I have urls that have hash tags / jump to id values appended to them. I want to check if the url has a hash tag and use that value as a click event like i would do on the page when clicking a button.
example url: http://domain./#help-video-modal-tags
jquery code:
jQuery(document).ready(function() {
if ( window.location.hash ) {
function showVideo() {
alert('in the function');
}
jQuery(window.location.hash).click(showVideo);
}
});
current click event triggers function
showVideo();
Share
Improve this question
asked Apr 22, 2014 at 13:12
esternleesternle
331 silver badge9 bronze badges
3
-
What do you mean by
current click event triggers function
? – Satpal Commented Apr 22, 2014 at 13:17 - Do you actually want to TRIGGER a click or do you just want to add a click event listener? Your code does the latter. So if you click on the element with the same id as the hash, it should work. – devnull69 Commented Apr 22, 2014 at 13:18
- the page has a click event that is also on a <a> link that will run the showVideo() function. I basically want to run that same action of clicking the <a> link but when the page loads by checking the hash value in the url. – esternle Commented Apr 22, 2014 at 13:19
2 Answers
Reset to default 3Use this
jQuery(document).ready(function() {
if ( window.location.hash ) {
function showVideo(hash) {
alert('in the function, hash -> ' + hash);
}
jQuery(document).click(showVideo(window.location.hash));
}
});
If I remember correctly to trigger an event of certain element is:
$('elem').trigger('click');
So in this case:
$(document).ready(function() {
if ( window.location.hash ) {
$(window.location.hash).trigger('click');//and trigger the event when needed
}
});
function showVideo() {
alert('do something when click is trigger');
}
$('elem').click(showVideo); //assign the event function normally
I have looked at a lot of examples on SO but can't seems to see what the issue is with my code:
I have urls that have hash tags / jump to id values appended to them. I want to check if the url has a hash tag and use that value as a click event like i would do on the page when clicking a button.
example url:
jquery code:
jQuery(document).ready(function() {
if ( window.location.hash ) {
function showVideo() {
alert('in the function');
}
jQuery(window.location.hash).click(showVideo);
}
});
current click event triggers function
showVideo();
I have looked at a lot of examples on SO but can't seems to see what the issue is with my code:
I have urls that have hash tags / jump to id values appended to them. I want to check if the url has a hash tag and use that value as a click event like i would do on the page when clicking a button.
example url: http://domain./#help-video-modal-tags
jquery code:
jQuery(document).ready(function() {
if ( window.location.hash ) {
function showVideo() {
alert('in the function');
}
jQuery(window.location.hash).click(showVideo);
}
});
current click event triggers function
showVideo();
Share
Improve this question
asked Apr 22, 2014 at 13:12
esternleesternle
331 silver badge9 bronze badges
3
-
What do you mean by
current click event triggers function
? – Satpal Commented Apr 22, 2014 at 13:17 - Do you actually want to TRIGGER a click or do you just want to add a click event listener? Your code does the latter. So if you click on the element with the same id as the hash, it should work. – devnull69 Commented Apr 22, 2014 at 13:18
- the page has a click event that is also on a <a> link that will run the showVideo() function. I basically want to run that same action of clicking the <a> link but when the page loads by checking the hash value in the url. – esternle Commented Apr 22, 2014 at 13:19
2 Answers
Reset to default 3Use this
jQuery(document).ready(function() {
if ( window.location.hash ) {
function showVideo(hash) {
alert('in the function, hash -> ' + hash);
}
jQuery(document).click(showVideo(window.location.hash));
}
});
If I remember correctly to trigger an event of certain element is:
$('elem').trigger('click');
So in this case:
$(document).ready(function() {
if ( window.location.hash ) {
$(window.location.hash).trigger('click');//and trigger the event when needed
}
});
function showVideo() {
alert('do something when click is trigger');
}
$('elem').click(showVideo); //assign the event function normally
本文标签: javascriptjquery detect hash in url trigger click eventStack Overflow
版权声明:本文标题:javascript - jquery detect hash in url trigger click event - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745661616a2161923.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论