Just define the function in its own separate JS file, and load that file alongside the page-specific files. The “WordPress way” would be to register the script containing the shared function(s) and then set it as a dependency of the other scripts:
wp_register_script( 'my-functions', get_theme_file_uri( 'js/functions.js' ) );
if ( is_page( 1 ) ) {
wp_enqueue_script( 'my-page-1', get_theme_file_uri( 'js/page-1.js' ), [ 'my-functions' ] );
}
if ( is_page( 2 ) ) {
wp_enqueue_script( 'my-page-2', get_theme_file_uri( 'js/page-2.js' ), [ 'my-functions' ] );
}
// etc.
Related Posts:
- How to make a word underline in Markdown
- Creating multiline strings in JavaScript
- Babel 6 regeneratorRuntime is not defined
- What is JSONP, and why was it created?
- React – uncaught TypeError: Cannot read property ‘setState’ of undefined
- Cannot read property ‘push’ of undefined when combining arrays
- SyntaxError: Unexpected token o in JSON at position 1
- Remove duplicate values from JS array
- Add swipe left/right to web page, but use default swipe up/down
- Check if checkbox is checked with jQuery
- What does “res.render” do, and what does the html file look like?
- How to pause javascript code execution for 2 seconds [duplicate]
- Getting Error “Form submission canceled because the form is not connected”
- How to implement navbar using react
- How to Create simple drag and Drop in angularjs
- stop all instances of node.js server
- Working with $scope.$emit and $scope.$on
- Using Node.js require vs. ES6 import/export
- How can jQuery deferred be used?
- Uncaught TypeError : cannot read property ‘replace’ of undefined In Grid
- Disable/enable an input with jQuery?
- How do I change an HTML selected option using JavaScript?
- How to refresh a Page using react-route Link
- ChangeDate – Date Picker Bootstrap
- Is there a sleep function in JavaScript?
- scrollIntoView Scrolls just too far
- How to set the id attribute of a HTML element dynamically with angularjs (1.x)?
- Using $setValidity inside a Controller
- Combination of async function + await + setTimeout
- How to go from one page to another page using javascript?
- How to update array value javascript?
- Javascript setInterval not working
- Javascript Regular Expression Remove Spaces
- image.onload event and browser cache
- Simulate Keypress With jQuery
- react router v^4.0.0 Uncaught TypeError: Cannot read property ‘location’ of undefined
- What’s the fastest way to square a number in JavaScript?
- JavaScript: Create and save file
- Adding options to a
- How to use scientific notation in js?
- String.Format not work in TypeScript
- jQuery Ajax POST example with PHP
- How to validate a credit card number
- TypeError: res.status is not a function
- Why am i receiving this jsfiddle error, document.write can be a form of eval
- jquery beforeunload when closing (not leaving) the page?
- Array Mapping in AngularJs
- How do I pass the this context to a function?
- HTML5 rich-text inside textarea
- Angular 4 setTimeout() with variable delay and wait
- JSON.parse unexpected token s
- Escaping HTML strings with jQuery
- AngularJS errors: Blocked loading resource from url not allowed by $sceDelegate policy
- Change an image with onclick()
- javascript return true or return false when and how to use it?
- Uncaught SyntaxError: Unexpected token with JSON.parse
- Why is this simple AngularJS ng-show not working?
- How to open a Bootstrap modal window using jQuery?
- window.location.replace() not working to redirect browser
- “Cross origin requests are only supported for HTTP.” error when loading a local file
- How to destroy a JavaScript object?
- How can I convert an image into Base64 string using JavaScript?
- Is there a way to create interfaces in ES6 / Node 4?
- wp_enqueue_script : how to change loading order of scripts?
- How to load translation in JavaScript?
- Is there a core Sortable component in Gutenberg?
- wp_register_script multiple identifiers?
- require.js to load javascript
- Is there an equivalent of the PHP function sanitize_key in Gutenberg?
- Add custom form to theme
- Setting HTML properties in a Gutenberg plugin using WordPress settings
- Is it mandatory to enqueue any kind of Javsacript?
- Script will not print in head if path to file is correct
- customize_preview_init: listening for any changes from Customizer preview area
- Add toggle button for mobile sub-menus
- Using webpack to add customizer live preview functionality – wp.customize is not a function
- Path to image in js with wp_localize_script [closed]
- disable tags on wordpress text editor
- Change background image based on the hour [closed]
- is_front_page() variable not found
- How to modify the paths of js from twentytwelve theme?
- Get terms of a taxonomy using useSelect
- What to do about no-global-event-listener warning for “DOM is ready” function?
- find out reason of “Updating failed” in Post-editor
- Unable To Call Javascript With Enqueue
- Getting a variable inside foreach from PHP to JS after localization
- How to integrate a JS fiddle?
- Problem after upgrade to 3.6.1
- Hovercards and other hover over image functions don’t work anymore!
- Load JavaScript on specific page with @wordpress compiler
- How WP does Mortgage affordability calculation
- java console error about touch navigation
- When adding google tag script in website, functionality not working
- How to improve WP-Rest atrocious response time?
- How should I identify the inline javascript that is the dependent of a wp_enqueue_script?
- How do I add a class to Customizer preview based on class of Customizer control? (Customizer Javascript API)
- How to verify that a field has a value before adding a term to a taxonomy
- Open image gallery on link click
- WordPress 4.2 mce-views migration guide?
- Use add_action to run a script, but only on the post editor page