admin管理员组文章数量:1333424
I have the following function and I want to execute it only on specific pages in my Wordpress website. I tried with if (is_page('pagename') but I cannot get it to work. Any suggestions?
$(function() {
$('a[href*=\\#]:not([href=\\#])').click(function() {
if (location.pathname.replace(/^\//, '') === this.pathname.replace(/^\//, '') && location.hostname === this.hostname) {
var target = $(this.hash);
target = target.length ? target : $('[name=' + this.hash.slice(1) + ']');
if (target.length) {
$('html,body').animate({
scrollTop: target.offset().top
}, 2000);
return false;
}
}
});
});
}
})
;
I have the following function and I want to execute it only on specific pages in my Wordpress website. I tried with if (is_page('pagename') but I cannot get it to work. Any suggestions?
$(function() {
$('a[href*=\\#]:not([href=\\#])').click(function() {
if (location.pathname.replace(/^\//, '') === this.pathname.replace(/^\//, '') && location.hostname === this.hostname) {
var target = $(this.hash);
target = target.length ? target : $('[name=' + this.hash.slice(1) + ']');
if (target.length) {
$('html,body').animate({
scrollTop: target.offset().top
}, 2000);
return false;
}
}
});
});
}
})
;
Share
Improve this question
asked Jun 5, 2018 at 5:59
Roumen StratievRoumen Stratiev
111 silver badge2 bronze badges
2 Answers
Reset to default 5Something like this will work:
if (window.location.pathname=='/account') {
}
create a javascript file contained this script. then import the script to the pages those you want to execute this in that page by adding
<script src="URL/TO/YOUR/SCRIPT/nameOfScript.js"></script>
this into the end of the body part or to the header of the html
本文标签: How to run a javascript function only on a certain page or pagesStack Overflow
版权声明:本文标题:How to run a javascript function only on a certain page or pages? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742303297a2449418.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论