'How to run specific code javascript DOM in page at site [closed]
I want help please :(
I have several dom codes, in a js file, running on a site site.com/script.js
I want to customize a specific code, to work only on a sub-page in the site.
for example:
the codes work on site.com and I want a specific code that works
only on the site.com/page16
and code javascript just run by one file I dont have any access to html page
Solution 1:[1]
You can access the end part of your url by using window.location.pathname.
For the website site.com/page16 - window.location.pathname returns the string "/page16".
Next you could have a function that checks to see what the current pathname is and terminates early if the pathname doesn't match /page16.
For example:
const subPageFn = () => {
// terminates early if subpage doesn't match page16
if (window.location.pathname !== '/page16') {
return
}
// runs page16 specific code here...
}
Then you'd call the function in any page by using
subPageFn()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | John Obla |
