javascript - onclick not working since move from HTML to WP

admin2025-01-08  6

I built a new site using Divi in WP. The previous HTML site had used an onclick event and also included the following script immediately following the opening tag. The digital marketing team informed me that after the move to WP, they no longer were receiving any data. I am trying to figure out where I have gone wrong as I am newer to WP. Any suggestions?

the onclick itself:

 <a onclick="goog_report_conversion ('javascript:document.searchform.submit();')"
href="javascript:document.searchform.submit();"  class="btn_reserve">Reserve Now!</a>

the JS:

<!-- Google Code for Reserve Now -->
<script type="text/javascript">
  /* <![CDATA[ */
  goog_snippet_vars = function() {
    var w = window;
    w.google_conversion_id = 973012345;
    w.google_conversion_label = "iNr5DMy-uHsQbyT-zwM";
    w.google_remarketing_only = false;
  }
  // DO NOT CHANGE THE CODE BELOW.
  goog_report_conversion = function(url) {
    goog_snippet_vars();
    window.google_conversion_format = "3";
    var opt = new Object();
    opt.onload_callback = function() {
    if (typeof(url) != 'undefined') {
      window.location = url;
    }
  }
  var conv_handler = window['google_trackConversion'];
  if (typeof(conv_handler) == 'function') {
    conv_handler(opt);
  }
}
/* ]]&gt; */
</script> 
<script type="text/javascript"
  src="//www.googleadservices/pagead/conversion_async.js">
</script>

I built a new site using Divi in WP. The previous HTML site had used an onclick event and also included the following script immediately following the opening tag. The digital marketing team informed me that after the move to WP, they no longer were receiving any data. I am trying to figure out where I have gone wrong as I am newer to WP. Any suggestions?

the onclick itself:

 <a onclick="goog_report_conversion ('javascript:document.searchform.submit();')"
href="javascript:document.searchform.submit();"  class="btn_reserve">Reserve Now!</a>

the JS:

<!-- Google Code for Reserve Now -->
<script type="text/javascript">
  /* <![CDATA[ */
  goog_snippet_vars = function() {
    var w = window;
    w.google_conversion_id = 973012345;
    w.google_conversion_label = "iNr5DMy-uHsQbyT-zwM";
    w.google_remarketing_only = false;
  }
  // DO NOT CHANGE THE CODE BELOW.
  goog_report_conversion = function(url) {
    goog_snippet_vars();
    window.google_conversion_format = "3";
    var opt = new Object();
    opt.onload_callback = function() {
    if (typeof(url) != 'undefined') {
      window.location = url;
    }
  }
  var conv_handler = window['google_trackConversion'];
  if (typeof(conv_handler) == 'function') {
    conv_handler(opt);
  }
}
/* ]]&gt; */
</script> 
<script type="text/javascript"
  src="//www.googleadservices.com/pagead/conversion_async.js">
</script>
Share Improve this question edited Mar 22, 2022 at 11:57 Cody Gray 1033 bronze badges asked Mar 22, 2022 at 11:54 JackieMJackieM 11 bronze badge 2
  • Where/how have you added this code? If you inspect the source of the page do you see it where you expect to see it? Any errors in the console? – Jacob Peattie Commented Mar 22, 2022 at 12:19
  • I used the WP Coder plugin to add the form code which includes the onclick and the actual form itself is where it should be and functioning properly. I can't get he JS to load immediately after the opening <body> tag and have moved it around several times to no avail. I'm wondering if the placement of the script is why the onclick isn't working? the url for the page is: parkbytheports.com/lp-cruise-parking in case you want to take a look. Thanks! – JackieM Commented Mar 22, 2022 at 12:29
Add a comment  | 

1 Answer 1

Reset to default 0

I would try separating the html from the javascript. i.e. not using "onclick" in html but instead using addEventListener.

<a href="javascript:document.searchform.submit();"  class="btn_reserve">Reserve Now!</a>

<script>
    document.querySelector('.btn_reserve').addEventListener("click", function(e) {

        // it seems like this line won't return a URL and will instead submit the form. 
        // I would double check it.
        var url = document.searchform.submit(); 

        goog_report_conversion(url);
    });
</script>
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736267382a1192.html

最新回复(0)