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

81
Visualizações
JS Function from URL

please help me. I have a code inside any of my posts.

post link is

link1    https://mysite.ext/mypost.html
link2    https://mysite.ext/mypost.html?id0987

    <script>
 let newuserid = window.location.search;
 if (newuserid = id0987) {
    aleret('Welcome, You reached throught ref id 0987');
 }
 </script>

I'm new to Code Life and I just create something like this. If someone comes up with 'link2' then run this function otherwise do nothing. The definition code is just one example.

Explain that if the URL of a post is post-url? Idxxxx. Then run the function xxxx. If the post url? Idyyyy Then run the function yyyy. The js code functions are already written in the current post. I know we can't create a function from a URL but a pre-written script can be run if the URL is like this do this. And in the URL we will track a referral ID.

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

Give this a try

<script>
let urlID = window.location.href.indexOf('id0987');
if (urlID != '-1'){
alert('Welcome, You reached throught ref id 0987');
}
</script>
about 4 years ago · Juan Pablo Isaza Relatório

0

You should put quotes here newuserid == "id0987"!
I have also put .substr(1) at the end. It removes ? from the url.

window.location.search => "?id0987"
window.location.search.substr(1) >=> "id0987"

<script>
let newuserid = window.location.search.substr(1);
if (newuserid == "id0987") { //Here!!
   window.alert('Welcome, You reached throught ref id 0987');
}
</script>
about 4 years ago · Juan Pablo Isaza Relatório

0

you're using the 'if' statement. when you compare whether a value is equal, you need to use == and not =.

therefore:

 let newuserid = window.location.search;
 if (newuserid == 'id0987') {
    alert('Welcome, You reached throught ref id 0987');
 }
 </script>

would probably make your code work. notice i also wrapped id0987 with '' and changed aleret to alert.

another tip - in javascript you usually perform equals assertion using === - the triple equate operator makes sure that the type of the value is also the same.

but that's for a different question.

about 4 years ago · Juan Pablo Isaza Relatório
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