Initialize the Scroll to Top script from within another JavaScript file. I’m assuming that jquery.backtotoplink.js
is coming from a third party and that it should not be modified.
For example, in a theme, the JavaScript could be enqueued like this:
function wpse247853_scripts() {
// jquery.backtotoplink.js depends on jquery
wp_enqueue_script( 'scrolltotop', get_template_directory_uri() .
'/js/jquery.backtotoplink.js', array( 'jquery' ), false, true );
// Enqueue a general purpose JS file here. It will initialize the scroll
// to top script. It depends on jquery and scrolltotop.
wp_enqueue_script( 'wpse-theme-js', get_template_directory_uri() .
'/js/wpse-theme.js', array( 'jquery', 'scrolltotop' ), false, true );
}
add_action( 'wp_enqueue_scripts', 'wpse247853_scripts' );
Initialize the scroll to top script here:
/js/wpse-theme.js
( function( $ ){
// Initialize the scroll to top plugin
$.backToTopLink( {} );
// ...Do some other jQuery stuff.
})( jQuery );
The code that was previously used to intialize the scroll to top script should be removed:
<script> $.backToTopLink({}); </script>