When the browser is refreshed, jQuery will automatically scroll the window down

I created a div that matches the height and width of the window to simulate a "home screen." When scrolling down to view the content, everything works fine. However, after refreshing the browser, it automatically scrolls back to where you were before. I want it to start at the top of the page instead. Any suggestions on how to fix this?

Here is the code I'm using:

            <!doctype html>
    <html>
    <head>
    <meta charset="utf-8">
    <title>Untitled Document</title>
    <script src="jquery/jquery-1.11.2.min.js"></script>
    </head>

    <style>
    html,body,p,h1,h2,div,section,img{
        margin:0;
        padding:0;

    }
    html,body{
    background:rgba(255,255,255,1.00);
    }
    header{
    background-color:rgba(181,255,0,0.51);
    width:100%;
    /* flex container */
    display: flex;
    flex-flow: row no-wrap;
    justify-content: center;
    }
    section{
        text-align:center;
    }

    </style>
    <body>
    <header></header>
    <section>
    <h2>Text</h2>
    <p>"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."</p>
    </section>

    </body>
    <script>
        $(document).ready(function(){
            $(window).scroll(function(){
                console.log($(window).scrollTop());
                });

            //screen size
            var windowh = $(window).height();
            var windoww = $(window).width();
            //header full screen
            console.log(windowh + ' '+windoww);
            $('header').css({
                'height': windowh,  
            });

        });





    </script>
    </html>

Answer №1

My theory is that the issue could be related to either cache or cookies, but I'm inclined to think this may prevent the website from experiencing those difficulties...

$(window).on('beforeunload', function(){
    $(window).scrollTop(0);
});

Answer №2

Why not give this a go:

$(document).ready(function(){
        $(this).scrollTop(0);
        $('html').animate({scrollTop:0}, 1);
        $('body').animate({scrollTop:0}, 1);
    });

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

"Before the click even happens, the jquery click event is already

As I was developing a small todo app, I ran into an issue where the alert message seemed to pop up before the click event was triggered. It almost seems like the event is not registered properly. Can someone explain why this is happening? (function() { ...

Adjusting Header Size While Scrolling in Angular

Looking for assistance with AngularJS regarding handling events (specifically scrolling). I am trying to dynamically change the size of the header based on whether the user scrolls up or down. Below is a snippet of JavaScript code that achieves this effect ...

How can I retrieve the value of a radio button nested within a label inside a div using the document.ready function in jQuery?

Even though I have encountered similar questions, none of the provided answers seem to work for me. As a data engineer with limited web development knowledge, I find myself struggling. Below is a code snippet containing a radio button that I am currently d ...

"PHP and Ajax not communicating properly - why aren't my JavaScript events triggering

Seeking help from you all as I'm facing a challenge with my code. I am attempting to make a simple ajax post request to a php file. The ajax request seems to be functioning correctly as I receive valid data back from my php script (confirmed by echoi ...

The AJAX response shows a value of "undefined"

My HTML page contains these codes, which display a list of employees from the database. <!DOCTYPE html> <html> <head> <title></title> <meta charset="utf-8" /> <script src="Scripts/jquery-1.10.2.js"></script> ...

Triggering onClick without interfering with its populated variable

I'd like to add the following code snippet to my document: $('#myDiv).append("<div id='myDiv2' onclick="+extElementConfig.onClickDo+">Do</div>"); The code above uses an object with properties to populate the onClick attrib ...

Is it possible to shift the image within the <canvas> element without having to redraw the entire canvas?

I created a game board and I am trying to implement drag-and-drop functionality for my pieces, which are in .gif format. However, I am struggling with finding a solution that allows me to move the pieces without constantly redrawing the entire board. Cur ...

Unexpected problem discovered with Firefox and JqueryUI - content is misaligned within the dialog box

I seem to be having an issue with a dialog that I recently set up. I've been working with jqueryUi for quite a while so I'm not sure why I can't figure this out. When using Firefox, I noticed that the dialog opens and closes when clicking " ...

Utilizing CodeIgniter for Efficient AJAX Posting

I'm struggling to submit a post using the ajax post method. It appears that the request is being posted without any error messages, but the model doesn't seem to respond and insert the row into the database. While I'm not well versed in jQu ...

Using Responsive Design with Bootstrap and Media Queries

As a beginner in front-end development, I have recently learned about media queries and their functionality. Having previously studied Bootstrap, I can't help but wonder if media queries are actually necessary. After all, Bootstrap seems to offer simi ...

Learning the ins and outs of HTML5 and CSS3

Does anyone have recommendations for high-quality HTML5 and CSS3 tutorials? ...

Detail row in DataTables without using nesting

I'm currently in the process of creating a table with nested data. My goal is to maintain a single set of column headers at the top of the table that extend throughout. In order to achieve this, I have begun modifying the Row Details example to suit m ...

Decrease the distance between hyperlinks

I understand that this question has been asked numerous times before, but as a beginner in HTML, I still require some assistance. Given the code provided below, how can I decrease the spacing between the links in each column? Code: a { color: white; ...

Is there a way to eliminate a CSS class from a div element within the outcome of an ajax response?

My ajax response is functioning properly and displaying the content of the result, but I am always getting the CSS effects which I do not want. I need to eliminate the class="container body-content" from the following div in the ajax call's result. ...

Customize a jQuery user interface dialog for interactive Ajax modal windows

Currently, I am working on implementing modal windows that dynamically load content using AJAX with the jQuery UI dialog widget. My goal is to only have the dialog present on the page when a user requests it, and then remove it when the user closes it. How ...

Looking to link a click event to a particular hyperlink?

I have a question about attaching click events to hyperlinks. Here is an example code snippet: $(".notice").click(function() { alert("Hello world!"); }); Below is the HTML for my hyperlink: <a href="http://www.bba-reman.com/catalogue/DetailedProdu ...

How to Create a Floating DIV with CSS

Here is the URL I am referring to: Website I want to position the Weather DIV above other content on the page while still maintaining its position when expanded or collapsed. How can I achieve this without affecting the layout of other elements? This is ...

Is it possible to insert additional lines into the default HTML file using VS Code?

Is it possible to customize the HTML template generated automatically by VS Code? For example, when I type html:5 + TAB, I would like the resulting HTML file to include predefined "style" and "script" tags along with some common code snippets that I regu ...

Determine if a certain value is present in an array within an HTML document

In the context of AngularJS, I have an array that looks like this: var test=["abc/1/1","def/2/2","efg/3/3"]; I am trying to determine if the array contains a value with "abc" in it within my HTML code. When I used the following: test.indexOf("abc")!=-1 ...

What is the significance of the "#" character in the URL of a jQuery mobile

As I encounter a strange issue with my jQuery mobile page, I notice that when accessing page.php everything looks good, but once adding #someDetailsHere to the URL, it only displays a blank white page. What could be causing this and how can I resolve it? ...