Is there a way to automatically adjust viewport meta values (such as initial scale, max scale, or min scale) with each button click?

Currently, I am in the process of developing a phonegap app. Within this app, I have utilized meta tag values using Javascript. I have a specific requirement to modify these meta tag values (such as initial scale, max scale, or min scale) upon clicking certain buttons. However, I have encountered an issue where the changes only occur once and then do not work thereafter. To provide some context, I am testing this app on Android 4.0.4 devices and employing Phonegap 2.9.0.

$(document).ready(function() {
    alert("Onload");
});
var scale = 1.0;
function zoomIn(){
    scale++;
    if (screen.width < 500) {

        $('#meta-viewport').attr('content', 'width=device-width, initial-scale=' + scale
        + ', maximum-scale=' + scale + ', minimum-scale=' + scale + ', user-scalable=yes');
    }
}

function zoomOut() {
    scale--;
    if (screen.width < 500) {

        $('#meta-viewport').attr('content', 'width=device-width, initial-scale=' + scale
        + ', maximum-scale=' + scale + ', minimum-scale=' + scale + ', user-scalable=yes');
    }
}

Here is a snippet of my HTML code:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="user-scalable=yes, initial-scale=1.0, maximum scale=1.0,
            minimum-scale=1.0,width=device-width,
            height=device-height,target-densitydpi=devicedpi" />
        <script type="text/javascript" src="js/jquery_v1.9.1.js"></script>
    </head>
    <body>
        <img id="background" src="img/bgImg.png" style="position: absolute; z-index:-1;" />
        <img id="carImg1" src="img/up.png"
            style="position: absolute; left: 150px; top: 100px; z-index:1;" />
    </body>
</html>

I would greatly appreciate any assistance in resolving this issue. Please advise on what may be causing the problem. Thank you!

Answer №1

  1. You appear to be concealing the original declaration of scale by redeclaring it within the two if statements. Consider removing the var keyword (or the entire statement) within your function declarations. In JavaScript, this will treat them as if they were declared at the top of the function.

  2. Modify the selectors from #meta-viewport to meta[name="viewport"].

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

Duration of video exceeds recorded time - Android Studio

Just dipping my toes into Android programming and I stumbled upon a curious issue while trying to record videos in my app. Quite baffled by the fact that although I recorded a 10-second video, the duration is showing as 67818 seconds! ...

Guidance on editing list items individually using jQuery from separate input fields

Working with li presents some challenges for me. On my webpage, I have an input field that dynamically adds values to a list. However, the function to edit these values is not working properly. After editing an li element, it deletes all classes and span ...

Angular 9 causing issues with Bootstrap accordion functionality

I'm currently working on a project with Angular 9. I have installed Bootstrap and jQuery, and the entries in my package.json look like this: "bootstrap": "^4.4.1", "jquery": "^3.5.0", I have imported the CSS and JS files in angular.json as ...

Can you explain the distinction between a standard $.post() method sending strings versus one sending a file?

Curious as to why I am unable to upload a file through JQuery using the $.post() function? Are there any modifications that can be made to the request in order to enable file uploads? $.post(url, { file: fileName, path: "/uploaded_files" }, function (res ...

Avoid tampering with forms in PHP, JavaScript, and JQuery to maintain security measures for PayPal transactions

Users have the option to purchase credits on my form using PayPal or bank transfer as payment methods. If a user chooses "PayPal," the form data will be sent to PayPal using JQuery/JS: $(':radio').change(function () { var $this = $(this).val(); ...

Create a custom loading spinner for a jQuery AJAX request

How can I add a loading indicator to my Bootstrap modal that is launched from a link? Currently, there is a 3-second delay while the AJAX query fetches data from the database. Does Twitter Bootstrap have built-in functionality for this? UPDATE: Modified J ...

The functionality of displaying and hiding elements in HTML using jQuery is not functioning as expected

When I implemented the .toggle() functionality in jQuery on the content, I encountered a problem where clicking on the "read more" button caused the text link to disappear and the first paragraph to become invisible. Below is the jQuery code snippet: $( ...

Techniques for incorporating a variable into the value field in JavaScript

let y = data[1]; cell1.innerHTML ='<input id="text" type="text" value= "'y'"/>' ; This snippet of code does not render any content when attempting to pass the variable, but if you provide a specific value like "h", it will displa ...

Determining the date based on user input

I am currently working on developing a Date Calculator widget that can generate dates based on user inputs to various fields. The idea is to allow users to find the date of an event that occurred a certain number of months/years/days ago by simply inputtin ...

What is the best way to display a Node.js Buffer in binary format?

I'm working with a Buffer object that looks like this <Buffer FF 00> Can anyone advise on how I can display it as real binary numbers, similar to: 11111111 00000000 ...

Can someone provide a clarification on the meaning of this Javascript code snippet?

I stumbled upon the code snippet below: let customHandler; clearTimeout(customHandler); customHandler = setTimeout(() => {...}); This code example is actually part of a Vue application and consists of the following method: public handleMultiSelectIn ...

What is causing the error when trying to parse a JSON with multiple properties?

Snippet: let data = JSON.parse('{"name":"dibya","company":"wipro"}'); Error Message : An error occurred while trying to parse the JSON data. The console displays "Uncaught SyntaxError: Unexpected end of JSON input" at line 1, character 6. ...

Refresh stock value in anychart without having to re-render the entire chart

I am currently experimenting with the Anychart stock candlestick chart and I must say, it is quite impressive. However, I have encountered an issue while trying to update the chart using a setInterval function. The problem is that it re-plots the entire ch ...

Triggering a jQuery click event to showcase the hidden content

I am attempting to replicate the functionality seen on the website. The issue I am facing is related to the event_content class. I want to display only a limited amount of content initially, and then hide the excess content. Upon clicking the plus class, ...

Ways to invoke a next.js api that implements next-auth programmatically

In my next.js app, I have integrated next-auth which is set up to allow authentication using Facebook and Google as providers. Additionally, there are some endpoints located in the pages/api folder of the app. These endpoints are accessed from standard ne ...

Ways to retrieve the JSON key value pair in a customized format

I have a JSON array structured like this: var region= [{"af":"Africa"},{"as":"Asia"},{"au":"Australia"}] Within the framework I am using, the values of the above array are accessed in the following way: {for r in regions} <option value="${r}" &g ...

Complete the transparent logo by showing the progress of the page loading

I am pondering the idea of filling in a logo based on the percentage of the page load. My initial thoughts are to use a gif, but I wonder if syncing a gif with the % of page load is really possible. Another idea is to have 100 images that change out depen ...

Reload the page when the value reaches 1

Hello, I am currently working on creating a system where my index page refreshes when a value in my database is set to 1. However, I am having trouble with the code as only my index.php is not refreshing. index.php <script> interval_timer = setInt ...

Transforming FullCalendar (jquery) into asp.net (ashx)

///////////// Expert //////////////// $(document).ready(function() { var date = new Date(); var d = date.getDate(); var m = date.getMonth(); var y = date.getFullYear(); $('#calendar').fullCalendar({ ...

Parsing JSON data repeatedly using JavaScript within an HTML environment

The following code I found on a popular web development website works perfectly: <!DOCTYPE html> <html> <body> <h1>Customers</h1> <div id="id01"></div> <script> var xmlhttp = new XMLHttpRequest(); var url ...