Home > Mobile >  Script tag within a script tag?
Script tag within a script tag?

Time:12-09

I'm trying inject some code into squarespace using script tags. However, I am trying to use a script tag to use JS to check if the user is on the right page, and if so I want to render a script tag. However, I don't think it is possible to render a script tag within a script tag? Does anyone have any suggestions on how to get around this?

<script type="text/javascript">

window.location.href === "https://www.page.com/correct-page" ? <script language='JavaScript1.1' SRC="correct-source"></script> : null;

</script>

Thanks

CodePudding user response:

It's not possible to have script tag inside another script tag, but you can create a script tag by document.createElement and append it to the body like this:

 if(window.location.href === 'myURL') {
    var script = document.createElement('script');
    script.src = 'mySrc';
    document.body.append(script);
}

CodePudding user response:

When you use a script tag inside another script tag the html code will consider the end script tag of the inner one to be the closing tag of outer tag. Simply you can add \x3c instead of <

<script type="text/javascript">

window.location.href === "https://www.page.com/correct-page" ? \x3Cscript language='JavaScript1.1' SRC="correct-source">\x3Cscript> : null;

</script>

  • Related