Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

80
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda