向Web URL插入参数:第三方应用URL端点生成与JSP代码适配
Solution to Build Your Target Search URL
Got it, let's fix this for you. The issue here is that your target URL has query parameters both before and after the hash (#), which the default form submission won't handle correctly—hash fragments are processed client-side, so the browser would append your search parameter after the hash instead of where you need it.
Here's the modified JSP code that constructs the exact URL you want, with explanations of the key changes:
<form id="summon_form" method="GET" action="//webpage" target="_blank" _lpchecked="1" onsubmit="handleFormSubmit(event);"> <input type="text" onchange="${gooEvent}" id="srchRes" title="SearchResult" onfocus="if (this.value === 'Enter search term') this.value = '';" placeholder="Enter search term"> <!-- Add a submit button if you don't already have one in your form --> <button type="submit">Search</button> </form> <script> function handleFormSubmit(event) { // Stop the browser's default form submission behavior event.preventDefault(); // Grab the search term from your input field const searchTerm = document.getElementById('srchRes').value.trim(); // Optional: Validate the user entered something if (!searchTerm) { alert('Please enter a search term first!'); return; } // Encode the search term to handle special characters (spaces, &, ?, etc.) const encodedTerm = encodeURIComponent(searchTerm); // Build the full target URL with both sets of parameters const finalUrl = `https://webpage/search?s.q=${encodedTerm}#!/search?ho=t&fvf=IsFullText,true,f&l=en&q=${encodedTerm}`; // Open the URL in a new tab (matches your original target="_blank") window.open(finalUrl, '_blank'); // Keep your existing double-submit prevention logic doubleSubmit.allowSubmitAgainForForm(document.getElementById('summon_form')); } </script>
Key Adjustments Made:
- Custom Submit Handler: Replaced the default form submission with
handleFormSubmitto take full control of URL construction. - URL Encoding: Used
encodeURIComponent()to safely encode the search term—this prevents broken URLs when users enter special characters like spaces or ampersands. - Exact URL Construction: Manually builds the URL with your required parameters, inserting the encoded search term into both the pre-hash (
s.q) and post-hash (q) positions. - Preserved Original Behavior: Keeps the
target="_blank"functionality and your existingdoubleSubmitlogic intact.
Quick Notes:
- I filled in the truncated
onfocushandler with a common pattern (clearing placeholder text)—replace that with your actual existing logic if needed. - Ensure the
doubleSubmitfunction is loaded before this script runs, or adjust the script order in your page.
内容的提问来源于stack exchange,提问作者BeckTeck
相关产品推荐
相关产品推荐

