You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

基于Input标签生成DHL运单查询URL的JavaScript问题

Fixing DHL Tracking URL Generation in JavaScript

First, let’s align on the core goal: we need to take a DHL waybill number from an input field and generate a valid URL in this exact format:
https://nolp.dhl.de/nextt-online-public/en/search?piececode=YOUR_WAYBILL_NUMBER

Common Issues That Break URL Generation

Here are the most frequent mistakes that cause this to fail:

  • Typos in the base URL: Missing segments like nextt-online-public or using the wrong language path
  • Failing to retrieve input values correctly: Accidentally referencing the input element itself instead of its value property
  • String concatenation errors: Forgetting to properly combine the base URL and waybill number (e.g., missing the = after piececode)
  • Unvalidated input: Allowing empty or non-numeric values that create broken URLs

Working Solution

Let’s build a minimal, functional example that fixes these issues:

HTML

<label for="dhl-waybill">DHL Waybill Number:</label>
<input type="text" id="dhl-waybill" placeholder="Enter 10-18 digit waybill">
<button onclick="generateTrackingUrl()">Generate Tracking Link</button>
<p id="tracking-result"></p>

JavaScript

function generateTrackingUrl() {
  // Grab the input element and its value (trim whitespace to avoid errors)
  const waybillInput = document.getElementById('dhl-waybill');
  const waybillNumber = waybillInput.value.trim();
  
  // Basic validation: ensure input is not empty and numeric
  if (!waybillNumber || isNaN(waybillNumber)) {
    document.getElementById('tracking-result').textContent = "Please enter a valid numeric waybill number.";
    return;
  }
  
  // Construct the valid URL (use encodeURIComponent to handle any unexpected characters)
  const baseUrl = "https://nolp.dhl.de/nextt-online-public/en/search?piececode=";
  const trackingUrl = baseUrl + encodeURIComponent(waybillNumber);
  
  // Display the clickable link
  const resultContainer = document.getElementById('tracking-result');
  resultContainer.innerHTML = `<a href="${trackingUrl}" target="_blank">Track Your Shipment Now</a>`;
  
  // Optional: Auto-redirect instead of showing a link
  // window.location.href = trackingUrl;
}

Key Details to Verify in Your Code

If you’re adapting this to your existing setup, double-check these:

  1. The base URL matches exactly (no typos in the domain or path)
  2. You’re correctly accessing the input’s value property (not just the element)
  3. The parameter name is piececode (DHL’s required query parameter)
  4. Test with your example waybill 48484848484848484 to confirm the generated URL works manually

内容的提问来源于stack exchange,提问作者fefe

火山引擎 最新活动