Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

118
Views
How to Include Javascript inside document.write properly or is there a workaround?

I am forced to put code in a place where- when inserted, it applies to thousands of web pages at the same time.

I would like to only have a script work when a URL contains: "/pN2toKYZ/checkout" :

So i used window.location.href.indexOf and that seems to work well, but im having trouble after that.

Here is what I am trying, I have also tried escaping the starting'<' but it did not work:

<script>
if (window.location.href.indexOf('/pN2toKYZ/checkout') != -1) {
   document.write("<script>
  gtag('event', 'conversion', {'send_to': 'AW-834461893/ixLXCK-Wm6kDEMXB840D'});
 </script>
")
}
</script>

Note: the script i'm trying to place is a Google Adwords conversion tag. Google Adwords general script is rightfully in place before this code.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You're already running inside a script element - instead of trying to write a new script to the page, put the code you want to run directly:

<script>
if (window.location.href.indexOf('/pN2toKYZ/checkout') != -1) {
  gtag('event', 'conversion', {'send_to': 'AW-834461893/ixLXCK-Wm6kDEMXB840D'});
}
</script>
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!