Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

160
Visualizações
searchParams not working when called from inside CasperJS code

I would like to update a parameter of a URL so in node JS I have a code that works fine see below:

var injectCorrectIndexKey = function (url) {
    var href = new URL(url);
    href.searchParams.set('key', 'ZZZZZZZ');
    return href.toString();
};
var x = 'https://mywebsite.com?key=7E27728B15&name=%20CORP';
console.log(injectCorrectIndexKey(x));

I tried to run the same code with CasperJS as following :

var injectCorrectIndexKey = function (url) {
    var href = new URL(url);
    href.searchParams.set('key', 'ZZZZZZZ');
    return href.toString();
};
casper.start("https://google.com").then(function () {
    var x = 'https://mywebsite.com?key=7E27728B15&name=%20CORP';
    casper.echo((injectCorrectIndexKey(x));
});

I'm getting this error:

[debug] [phantom] url changed to "https://www.google.com/#spf=1637428890238"
[debug] [phantom] Successfully injected Casper client-side utilities
[debug] [phantom] start page is loaded
[info] [phantom] Step anonymous 3/3 https://www.google.com/#spf=1637428890238 (HTTP 200)
TypeError: undefined is not an object (evaluating 'href.searchParams.set')
  /var/www/phantomjs:/code/test.js:37 in injectCorrectIndexKey
  /var/www/phantomjs:/code/test.js:42
  /var/www/phantomjs:/platform/casper.js:1685 in runStep
  /var/www/phantomjs:/platform/casper.js:414 in checkStep
[debug] [phantom] Navigation requested: url=about:blank, type=Other, willNavigate=true, isMainFrame=true
[debug] [phantom] url changed to "about:blank"

Why is href.searchParams.set not working when I run it inside CasperJS? Isn't it valid JS code? Any idea please why it is not working as expected?

Thanks.


I solved my issue using this function to replace parameters.

var injectCorrectIndexKey = function (url, paramName, paramValue) {
    if (paramValue === null) {
        paramValue = '';
    }
    var pattern = new RegExp('\\b(' + paramName + '=).*?(&|#|$)');
    if (url.search(pattern) >= 0) {
        return url.replace(pattern, '$1' + paramValue + '$2');
    }
    url = url.replace(/[?#]$/, '');
    return url + (url.indexOf('?') > 0 ? '&' : '?') + paramName + '=' + paramValue;
};
about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda