简体   繁体   中英

How to add a script tag in nextjs with dangerouslysetinnerhtml?

I am trying to add.roll to my nextjs app. I used the script tag with dangerouslysetinnerhtml but it's not able to verify. What am I missing here?

This is the code

<Script`

{
adroll_adv_id = "xxxxxxxxxxxxxxxxxxxx";
    adroll_pix_id = "xxxxxxxxxxxxxxxxxxxx";
    adroll_version = "2.0";

    (function(w, d, e, o, a) {
        w.__adroll_loaded = true;
        w.adroll = w.adroll || [];
        w.adroll.f = [ 'setProperties', 'identify', 'track' ];
        var roundtripUrl = "https://s.adroll.com/j/" + adroll_adv_id
                + "/roundtrip.js";
        for (a = 0; a < w.adroll.f.length; a++) {
            w.adroll[w.adroll.f[a]] = w.adroll[w.adroll.f[a]] || (function(n) {
                return function() {
                    w.adroll.push([ n, arguments ])
                }
            })(w.adroll.f[a])
        }

        e = d.createElement('script');
        o = d.getElementsByTagName('script')[0];
        e.async = 1;
        e.src = roundtripUrl;
        o.parentNode.insertBefore(e, o);
    })(window, document);
    adroll.track("pageView");
}`

/>

Try this code it's works for me

import Script from 'next/script'
function MyApp() {
  return (
    <>
      <Script dangerouslySetInnerHTML={{
        __html: `Your script`}}></script>
    </>
  );
}

export default MyApp;

You can try this, it may works for you.

      <script
        dangerouslySetInnerHTML={{
            __html: `
                adroll_adv_id = "xxxxxxxxxxxxxxxxxxxx";
                adroll_pix_id = "xxxxxxxxxxxxxxxxxxxx";
                adroll_version = "2.0";
            
                (function(w, d, e, o, a) {
                    w.__adroll_loaded = true;
                    w.adroll = w.adroll || [];
                    w.adroll.f = [ 'setProperties', 'identify', 'track' ];
                    var roundtripUrl = "https://s.adroll.com/j/" + adroll_adv_id
                            + "/roundtrip.js";
                    for (a = 0; a < w.adroll.f.length; a++) {
                        w.adroll[w.adroll.f[a]] = w.adroll[w.adroll.f[a]] || (function(n) {
                            return function() {
                                w.adroll.push([ n, arguments ])
                            }
                        })(w.adroll.f[a])
                    }
            
                    e = d.createElement('script');
                    o = d.getElementsByTagName('script')[0];
                    e.async = 1;
                    e.src = roundtripUrl;
                    o.parentNode.insertBefore(e, o);
                })(window, document);
                adroll.track("pageView");
            `,
        }}
    ></script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM