> For the complete documentation index, see [llms.txt](https://ipforensics.gitbook.io/ipforensics/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://ipforensics.gitbook.io/ipforensics/code-implantation/html5-geolocation-with-ipforensics-fallback.md).

# HTML5 Geolocation with IpForensics fallback

An example of how you would use the ipdata API as a fallback for when HTML5 Geolocation fails or if the user blocks geolocation requests from your website

This example is adapted from the Wikipedia article on [W3C Geolocation](https://en.wikipedia.org/wiki/W3C_Geolocation_API). Note that it handles every error by making a call to the IpForensics API to get the lat/long.

Also note that instead of getting the `lat/long` (that is when you fall back to our service) and then using another service to geocode the `lat/long` to a place i.e. city or country. You could simply get the country, city, region, postal code and numerous other attributes from the `this.responseText` response object in the example below.

```javascript
const geoFindMe = () => {
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(success, error, geoOptions);
    } else {
        console.log("Geolocation services are not supported by your web browser.");
    }
}

const success = (position) => {
    const latitude = position.coords.latitude;
    const longitude = position.coords.longitude;
    const altitude = position.coords.altitude;
    const accuracy = position.coords.accuracy;
    console.log(`lat: ${latitude} long: ${longitude}`);
}

const error = (error) => {
    var request = new XMLHttpRequest();
    request.open('GET', 'https://ipforensics.net/api/v1/origin?apikey=APIKEY');
    request.setRequestHeader('Accept', 'application/json');
    request.onreadystatechange = function () {
      if (this.readyState === 4) {
        data = JSON.parse(this.responseText)
        console.log(`lat: ${data.meta_data.geolocation.latitude} long: ${data.meta_data.geolocation.}`);
      }
    };
    request.send();
}

const geoOptions = {
    enableHighAccuracy: true,
    maximumAge: 30000,
    timeout: 27000
};

// call the function
geoFindMe()
```

{% hint style="success" %}
**Always reference the** [**API endpoint**](/ipforensics/api/ip-and-threat-intelligence-api.md) **for complete response result**
{% endhint %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://ipforensics.gitbook.io/ipforensics/code-implantation/html5-geolocation-with-ipforensics-fallback.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
