Roumen Stratiev
Roumen Stratiev

Reputation: 11

How to run a javascript function only on a certain page or pages?

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;

                }

            }
        });

    });
}
})
;

Upvotes: 1

Views: 6730

Answers (2)

Thomas Amundsen
Thomas Amundsen

Reputation: 63

Something like this will work:

if (window.location.pathname=='/account') {

}

Upvotes: 5

noone
noone

Reputation: 6558

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

Upvotes: 2

Related Questions