Determining the height of a jQuery mobile page

Struggling for the last 24 hours to adjust the min-height styling on a jQuery mobile page specifically for mobile safari. Despite trying various methods like inline styles and overriding ui-page styles, I have not been successful in changing the height of data-role="page". My goal is to dynamically adjust the "page" height based on the content within it. Please refer to the illustration attached for a clearer understanding of the issue.

<div data-role="page">
     <div data-role="header">
             Header Elements
     </div>
     <div data-role="content" class="homeNav">
            <ul data-role="listview" data-inset="false" data-filter="false">
                <li><a href="expertise.html">Expertise</a></li>
                <li><a href="greatesthits.html">Greatest Hits</a></li>
                <li><a href="profile.html">Profile</a></li>
                <li><a href="mindset.html">Mindset</a></li>
                <li><a href="connect.html">Connect</a></li>
             </ul>  
     </div><!-- /content -->

     <div data-role="footer" data-position="fixed">
             Footer elements
     </div><!-- /footer -->
</div><!-- /page -->

Answer №1

By using JavaScript, the min-height for the data-role="page" element can be dynamically set within a resize event handling function associated with the window object. Users have the flexibility to customize their own resizing logic:

$(function () {
    $(window).bind('resize', function (event) {
        var content_height = $.mobile.activePage.children('[data-role="content"]').height(),
            header_height  = $.mobile.activePage.children('[data-role="header"]').height(),
            footer_height  = $.mobile.activePage.children('[data-role="footer"]').height(),
            window_height  = $(this).height();

        if (content_height < (window_height - header_height - footer_height)) {
            $.mobile.activePage.css('min-height', (content_height + header_height + footer_height));
            setTimeout(function () {
                $.mobile.activePage.children('[data-role="footer"]').css('top', 0);
            }, 500);
        }
        event.stopImmediatePropagation();
    }).trigger('resize');
});

Explore a live demonstration: http://jsfiddle.net/sAs5z/1/

An important aspect is utilizing the setTimeout method to adjust the positioning of the fixed-position-footer; potentially decreasing the delay interval could enhance user experience. This workaround tackles an issue where the jQuery Mobile Framework was readjusting the fixed-position-footer to the page's bottom erroneously. Refer to this example: http://jsfiddle.net/sAs5z/

Furthermore, it might be beneficial to solely reposition the fixed-position-footer while maintaining the original page min-height property. This adjustment ensures that the gradient backdrop extends across the entire screen without any spacing between the footer and content. Experiment with this approach in the following demonstration: http://jsfiddle.net/sAs5z/2/

Answer №2

Although this ticket is old, I have come up with a new solution that I find more preferable. My solution involves specifically unbinding the resize event using some jQuery internal techniques. I am not in favor of Jasper's solution as it relies on one event blocking another, and events should ideally be independent of each other without depending on the order in which they fire.

$(function() {
    // WARNING: Very advanced code ahead. This script hacks into jQuery mobile to reset page heights on resize.
    // We need to unbind the resize function ONLY and set all pages back to auto min-height.
    // This code works for jquery version 1.8.

    // First, reset all pages to default height
    $('[data-role="page"]').css('min-height', 'auto');

    // Check if we want to unbind this specific function
    var check = function(func) {
        var f = func.toLocaleString ? func.toLocaleString() : func.toString();
        // Verify if func.name matches or if the function body contains certain strings
        if(func.name === 'resetActivePageHeight' || (f.indexOf('padding-top') > -1 && f.indexOf('min-height'))) {
            return true;
        }
    };

    // Try to unbind the document pageshow event
    try {
        var dHandlers = $._data(document).events.pageshow;

        for(x = 0; x < dHandlers.length; x++) {
            if(check(dHandlers[x].handler)) {
                $(document).unbind('pageshow', dHandlers[x]);
                break;
            }
        }
    } catch(e) {}

    // Try to unbind the window handler
    try {
        var wHandlers = $._data(window).events.throttledresize;

        for(x = 0; x < wHandlers.length; x++) {
            if(check(wHandlers[x].handler)) {
                $(window).unbind('throttledresize', wHandlers[x]);
                break;
            }
        }
    } catch(e) {}
});

Answer №3

Dealing with a similar problem, I found that using $.mobile.resetActivePageHeight() while displaying a popup solved the issue for me. Thank you for sharing this helpful tip!

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

cross-domain ajax response

Imagine a unique scenario that will pique the interest of many developers. You have a JavaScript file and a PHP file - in the JS file, you've coded AJAX to send parameters via HTTP request to the PHP file and receive a response. Now, let's delve ...

Mastering the Art of Configuring Filters in Plupload

I am using plupload with a unique feature that allows me to select and upload files with extensions that are not defined in the settings. For instance, I can upload .rar or .txt files without explicitly defining them in the filters. $(".uploadDocs").cli ...

Adding data to a URL using jQuery Ajax

I'm currently working on implementing Ajax with jQuery and I have a specific requirement to add data to the URL. Take a look at the code snippet below: var my_website= mysamplesite.com/viewData/"; function displayData(myData) { $.ajax({ t ...

UI-Router: What is the best way to access a page within my project without adding it as a State?

Currently in the process of learning Angular and UI-Router. Initially, I followed the advice of many and chose to utilize UI-Router. In my original setup, the login page was included in all States. However, I decided it would be best to keep it as a separ ...

The Jquery.ajax function is passing null values to the controller, but it functions properly when using pre-defined

I have been working on a simple app in VS Code where I need to save values into a database. This is the code snippet I have written so far. var name = $("#TextName").val(); var price = $("#TextPrice").val(); var descrip ...

Issue with Jquery event not triggering correctly following ajax data retrieval

This script uses jQuery and Ajax to populate a navigation bar with categories and subcategories dynamically. The first unordered list is static, while the second one is populated after receiving data via Ajax. There are some jQuery events that need to be i ...

An issue occurred while attempting to read words from JSON file

My current issue involves loading words from JSON into my webpage. The images are functioning properly, thankfully. I have already successfully loaded the necessary images through JSON onto my webpage. However, I am still in need of loading words through ...

Tips for retrieving the text value on keyboard enter press without triggering form submission

I am currently working on a feature where hitting the Enter key in a textbox should not automatically submit the form. The textbox is located within a form: @Html.TextBoxFor(model => model.ID, new { @class = "form-control input-sm", placehold ...

"Utilize JSON autocomplete to transfer $_POST data to a separate webpage using

Currently, I am working on the select_course.php file which includes AJAX autocomplete. My goal is to pass the array variable obtained from get_course.php <script type="text/javascript"> $(document).ready(function(){ $("#course_no").auto ...

What steps can I take to eliminate the initial delay when it is first invoked?

My function is being called every 10 minutes like this: var interval = self.setInterval(my_func, 600000); function my_func(){ $.ajax({ url: 'xxxxxxxx', success: function(response) { var data= JSON.parse(response); dis ...

An easy CSS conundrum when hovering

Looking for some assistance with CSS. I want to display the text "Featured Page" with a picture appearing on the right side upon hovering (mouseover). Currently, the picture shows up under the text using the following CSS, but I need it to be larger and pl ...

Discovering the following element containing an ID attribute with jQuery

Upon clicking a link, my goal is to locate the subsequent <section> with an ID attribute and retrieve its ID. In the provided code snippet and JavaScript function, the expected outcome upon clicking the link is for "section_3" to be logged in the co ...

Guide for showing an alert message after submitting a form using PHP and jQuery

Everything seems to work well with the jQuery modal, but I'm struggling to figure out how to display an alert message when the form is successfully executed by PHP. $.post( 'name.php', { ime: ime, location: location }, functio ...

Building an event management system using CodeIgniter and jQuery to create a seamless scheduling calendar

I am in the process of creating an event and scheduling calendar using codeigniter and jquery. It is important for me to be able to have multiple events on the same day while keeping the design clean and user-friendly. If anyone has any recommendations fo ...

Adjusting the width of columns in a flex layout using Bootstrap 4

On mobile, I am attempting to rearrange some elements in a different order. Currently, this is what I have: https://i.stack.imgur.com/fY3PQ.png Below is the HTML code: <main> <div data-color="yellow"></div> <div class="wrapper"& ...

Stopping repeated content with endless scrolling ajax spinner

It seems like my mind is too foggy from the late night hours to find a clear solution, so I thought I'd seek some input from those who have an opinion... The website project I'm currently working on features a lengthy list of user posts. I' ...

Tips on prefixing Bootstrap 4 classes to prevent conflicts with CSS classes

Recently, I've been focusing on creating small applications for websites. The websites hosting these apps may or may not use a css framework. To prevent any potential conflicts, I have decided to add a unique prefix to all Bootstrap classes. For insta ...

The concept of CSS inheritance within div elements

I've been researching extensively about the concept of CSS inheritance, but I have come across a puzzling question that remains unanswered. Consider the code snippet below: <!DOCTYPE HTML> <html> <head> <style type="text/css"> ...

Replicating DIV element with input field

Once again, I find myself stuck in my attempt to duplicate a DIV and its form elements using jQuery. Button: <div class="addNew" id="addSkill">Add Skill <i class="icon-plus"></i></div> This is the Div and contents that I want to ...

The fixed width setting for the alpha table column is not being recognized

Despite my efforts, the responsive bootstrap 4 table is not adhering to my fixed column width styles. <table class="table table-responsive table-sm table-striped table-hover"> <thead> <tr> <th> <input typ ...