1

I'm using js to edit HTML and the code that I'm trying to run is bounded by quotations, which is using something bounded by apostrophes which in turn needs to use something bounded by quotations. the issue is that thinner quotations end the outer quotations prematurely. what do I do? (code below) see this question for more context, or if you have an alternative solution based on that

if(window.location == "https://www.hiring.example.com" || "https://example.hr/?"){
document.getElementsByTagName("a")[8].innerHTML = "<a href='/p/2066e45ed47a-commercial-real-estate-mortgage-broker/apply'><button class='button apply polygot button-right bzyButtonColor'>Apply</button><h2>Commercial Mortgage Broker — Apply Now</h2><ul class='meta'><li class='location'><i class='fa fa-wifi'></i><span class='polygot'>Remote OK</span></li><li class='type'><i class='fa fa-building'></i><span class='polygot'>Full-Time</span></li><li class='department'><i class='fa fa-building'></i><span>Multi-Family Group</span></li></ul><button class='button apply polygot button-full bzyButtonColor'>Apply</button><\a>";

document.getElementsByTagName("a")[1].innerHTML = "<a href='https://www.example.com/mfg' class='brand'><img src='https://gallery-cdn.breezy.hr/ab67d180-5ac3-43ce-8016-f80713fe5ebb/EU LOGO 3.jpg'></a>";

document.getElementsByTagName("a")[4].innerHTML = '<a href="/p/2066e45ed47a-commercial-real-estate-mortgage-broker/apply" style="background-color: rgba(0,0,0,0.2);<span class="polygot">APPLY NOW</span></a>';
}
else{
    var scrpt = document.createElement("script");
    scrpt.innerHTML = '<script>

        if(window.location == "https://www.hiring.easternunion.com/ || "https://example.hr/?" || "https://example.hr/"){

        }
        else{
        document.getElementsByTagName("a")[0].innerHTML = "<a href='https://www.example.com/mfg' class='brand'><img src='https://gallery-cdn.example.hr/ab67d180-5ac3-43ce-8016-f80713fe5ebb/EU LOGO 3.jpg'></a>";
        }
        </script>';
    document.body.appendChild(scrpt);

}
Antheloth
  • 113
  • 5
  • 1
    You can use backticks: `\``. Also that `window.location` check won't work like that, you need to state the full test between each `||` operator. –  Jul 27 '20 at 21:14
  • The best solution is to stop using inline JavaScript for event handling. Use `addEventListener` to have it call a function. – Barmar Jul 27 '20 at 21:24

1 Answers1

1

Escape your quotes, like so:

var a = '<a onclick="alert(\'scarecrow\');">alert</a>';

In theory you can do this to any level of nesting (so you don't even need two types of quote), but it quickly becomes a pain once you start needing to escape your backslashes with more backslashes.

Brilliand
  • 13,404
  • 6
  • 46
  • 58