I'm searching for a better way for this : I have several maps to display so I use a GPX file per map of course and also one HTML file per map; the HTML file invokes a .js routine which displays the GPX in google Maps.
The "issue" is that all these HTML files are identical except the name of the GPX file they call i.e.
Index
<a href="Itineraires/1.html">GPX</a>
<a href="Itineraires/2.html">GPX</a>
<a href="Itineraires/3.html">GPX</a>

1.html calls LoadGPXFileIntoGoogleMap(MyMap, "1.GPX")
2.html calls LoadGPXFileIntoGoogleMap(MyMap, "2.GPX")
...
My question : how can I invoque an unique HTML file passing to it the GPX file URL as parameter ?
Thanks a lot in advance

Dani AI

Generated

A single HTML file can replace the many near-identical pages by passing the GPX name in the URL (query string or hash). That keeps the map/display code in one place (as suggested) and answers the duplication problem raised by . Read the parameter on load, validate it against an allowlist, then hand the validated URL to the map loader.

Example client-side pattern:

const params = new URLSearchParams(window.location.search);
let gpx = params.get('gpx') || 'default.gpx';

// allowlist to prevent tampering/path traversal
const allowed = ['route1.gpx','route2.gpx','route3.gpx'];
if (!allowed.includes(gpx)) {
  console.warn('Invalid GPX:', gpx);
  gpx = 'default.gpx';
}

const gpxUrl = '/Itineraires/' + encodeURIComponent(gpx);
displayGpx(map, gpxUrl);

Use URLSearchParams to parse the query string and encodeURIComponent when building URLs. Do not accept arbitrary paths from the client; validate against a known list or map user-friendly IDs to filenames on the server to avoid path traversal (see OWASP on Path Traversal).

Alternatives and gotchas: use the hash fragment if only client-side routing is wanted, or use server rewrite rules to present clean URLs while internally mapping to the single HTML page. If GPX files are on another origin, watch CORS and check the Network panel for 404s or blocked requests. Quick checks: console errors, Network tab for the GPX request, correct path and encoding, and an allowlist fallback so a bad parameter never breaks the page.

Recommended Answers

All 3 Replies

Just put them all in the same file, and click the one you want.

Thanks but that's the goal, not the way of achieving it !

Put them, one after the other, in the same html file. You can use notepad to do this. Here is a sample of parts the finished file.

<body>
<h1>Index</h1>
<p>
  <a href="Itineraires/1.html">GPX1</a>
  <a href="Itineraires/2.html">GPX2</a>
  <a href="Itineraires/3.html">GPX3</a>
</p>
</body>

This displays:

GPX1 GPX2 GPX3

Each one is a clickable link (which I can't make blue, due to the stoopid color limitations on this site, so I made then green).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.