skip to Main Content

I’m using the extension "Custom right-click menu" to add a custom site search. When I use the context menu, it adds spaces to the search term. I need it to have a +.

var search = crmAPI.getSelection() || prompt('Please enter a search query');
var url    = 'https://www.subetalodge.us/list_all/search/%s/mode/name';
var toOpen = url.replace(/%s/g,search);
window.open(toOpen, '_blank');

2

Answers


  1. This correction should work:

    var toOpen = url.replace(/%s/g,search.replaceAll(' ', '+'))
    

    Whole code:

    var search = crmAPI.getSelection() || prompt('Please enter a search query'); var url = 'https://www.subetalodge.us/list_all/search/%s/mode/name'; var toOpen = url.replace(/%s/g,search.replaceAll(' ', '+')); window.open(toOpen, '_blank');
    
    Login or Signup to reply.
  2. To replace spaces with a + sign in the search term before constructing the URL, you can modify your code slightly to use the encodeURIComponent() function. This function will encode spaces as %20, which is equivalent to a + sign in URL encoding. Here’s how you can update your code:

    var search = crmAPI.getSelection() || prompt('Please enter a search query');
    // Replace spaces with '+'
    search = encodeURIComponent(search.trim()).replace(/%20/g, '+');
    var url = 'https://www.subetalodge.us/list_all/search/%s/mode/name';
    var toOpen = url.replace(/%s/g, search);
    window.open(toOpen, '_blank');
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search