How do i run a script ONLY when x.html is opened?

Hello ladies and gentlemen :slight_smile:

I wanted to ask how i can load a script on page load, but only when x.html is loaded? With this code i get an alert everytime i load any page from the menu on my site. How ever, i would only like a certain jQuery code to run when someone opens up “aboutme.html”. Then i would like for elements to move around and come to its place, which i will think about after this problem is solved.

window.onload = function () {
alert(“Hello World!”);
};

So how do i make the alert ONLY on 1 of my html pages? And ONLY run when the site is fully loaded? Sorry for this foolish question, sometimes you just can’t get your head around certain things…

Cheers!

Hey there,

you can read the current URL with window.location.
For the loading part, you can add an Event Listener for when the DOM content loaded.

Well, if I’m understanding you correctly you want to do do something when visiting a page and that page has fully loaded and elements have “moved around”.

The window.onload event handler fires when the page is fully loaded, and does not respect eventual css moving things around. So using it in this case would fire it before things have moved.

If your things moving took 3 seconds, you could use setTimeout in conjunction with window.onload to do your script after the page has fully loaded and things have moved.

window.onload = function(){
    setTimeout(function(){ 
        //doSomething();
    }, 3000);
}

You also mention only wanting to the script on one of your pages. Easiest way to do this is just to use a different js file for that html page.

Or to build on @miku86 use window.location to read the url and fire the script only if the user is on the right page .