tag * 3. The script will run automatically when the page loads */ (function() { 'use strict'; // Configuration: URL parameter to select option mapping // Format: 'url-parameter': 'Exact text of the option in the select' // Real usage example (uncomment and adjust as needed): const urlToOptionMap = { 'Honing': 'Honing', 'Cylinder Repair': 'Cylinder Repair', 'Custom-Built Cylinders': 'Custom-Built Cylinders', 'Expedited Repair': 'Expedited Repair', 'Machining': 'Machining', 'Rechroming': 'Rechroming', 'Welding': 'Welding' }; // ID of the select field (adjust if necessary) const selectFieldId = 'form-field-field_8386bdd'; /** * Gets the parameter from the current URL * Supports formats: ?parameter or ?param=value */ function getUrlParameter() { const urlParams = new URLSearchParams(window.location.search); // Checks each mapped key to see if it exists as a parameter for (const key in urlToOptionMap) { if (urlParams.has(key)) { return key; } } return null; } /** * Pre-selects the option in the select field */ function preselectOption(optionText) { const selectElement = document.getElementById(selectFieldId); if (!selectElement) { console.warn('Select field not found:', selectFieldId); return false; } // Searches for the option by its text content const options = selectElement.options; for (let i = 0; i < options.length; i++) { if (options[i].text.trim() === optionText.trim()) { selectElement.selectedIndex = i; // Triggers events to ensure frameworks detect the change selectElement.dispatchEvent(new Event('change', { bubbles: true })); selectElement.dispatchEvent(new Event('input', { bubbles: true })); console.log('Option pre-selected:', optionText); return true; } } console.warn('Option not found in select:', optionText); return false; } /** * Main function - executes the pre-selection */ function init() { // Waits for the DOM to be fully loaded if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', init); return; } // Gets the URL parameter const urlParam = getUrlParameter(); if (!urlParam) { console.log('No source parameter found in the URL'); return; } // Gets the corresponding option const optionToSelect = urlToOptionMap[urlParam]; if (!optionToSelect) { console.warn('Unmapped parameter:', urlParam); return; } // Tries to pre-select immediately let success = preselectOption(optionToSelect); // If it fails, tries again after a delay (for pages with async loading) if (!success) { setTimeout(function() { preselectOption(optionToSelect); }, 500); } } // Starts the script init(); })();