Issue with double quote and quotes

I am creating a list of links in javascript but it appears that the browser are translating some caracters

This is my code

    vLinea += "<a href='#' onclick='window.open('frmEnviarPorCorreo.aspx?Pr_Id=" + pData.Pr_Id + "'"
    vLinea += ", '_blank', 'location=yes,height=570,width=520,scrollbars=no,status=no');>"
    vLinea += "<img   class='imgShare'  src='images/icoEmail.png'>" 
    vLinea += "</a> </li>"

What I would like is this

<a href="#" onclick="window.open('frmenviarporcorreo.aspx?pr_id=12806','_blank','location=yes,height=570,width=520,scrollbars=no,status=no');"><img class="imgShare" src="images/icoEmail.png"></a>

What I am getting is this, Some ’ are translated as " some " are added I don’t know why

<a href="#" onclick="window.open(" frmenviarporcorreo.aspx?pr_id="12806'," '_blank',="" 'location="yes,height=570,width=520,scrollbars=no,status=no');"><img class="imgShare" src="images/icoEmail.png"></a>

Embedding HTML in your JavaScript (and vice-versa) is normally a bad idea.

However, the easiest way to achieve what you’re after is by using template literals (which use backticks to delimit the string), where variables can be placed within using the ${someVar} syntax.

This allows you to maintain your HTML code with doublequotes as per normal.

vLinea += `<a
    href="#"
    onclick="window.open(
      'frmEnviarPorCorreo.aspx?Pr_Id=${pData.Pr_Id}',
      '_blank',
      'location=yes,height=570,width=520,scrollbars=no,status=no')">
  <img class="imgShare" src="images/icoEmail.png"> 
</a>`;

It doesn’t have to be all readable like that. You can pack it up like this too:

vLinea += `<a href="#" onclick="window.open('frmEnviarPorCorreo.aspx?Pr_Id=${pData.Pr_Id}', '_blank', 'location=yes,height=570,width=520,scrollbars=no,status=no')">
  <img class="imgShare" src="images/icoEmail.png"> 
</a>`;

If you were to do this without using the template literal, you would have to use single quotes for the JavaScript strings, double quotes for the HTML strings, and escape any quotes within the HTML strings, which results in the following horror show:

vLinea += '<a href="#" onclick="window.open(\'frmEnviarPorCorreo.aspx?Pr_Id=' + pData.Pr_Id + '\'';
vLinea += ', \'_blank\', \'location=yes,height=570,width=520,scrollbars=no,status=no\')">';
vLinea += '<img class="imgShare" src="images/icoEmail.png">';
vLinea += '</a>';

I’m sure that can agree that this is the most horrendous solution out of all that we’ve seen thus far.

3 Likes

Normally we do not use HTML code within out scripting code, as it causes many maintenance headaches.

One possible way is to create the elements from within JavaScript itself.

function createEl(el, attrs) {
  var el = document.createElement(el);
  Object.keys(attrs).forEach(function(attr) {
    el.setAttribute(attr, attrs[attr]);
  });
  return el;
}

var pData = {
  Pr_Id: 1234
};

var els = document.createDocumentFragment();
var url = "frmEnviarPorCorreo.aspx?Pr_id=" + pData.Pr_Id;
var a = createEl("A", {
  href: "#",
  onclick: "window.open('" + url + "')"
});
var img = createEl("IMG", {
  class: "imgShare",
  src: "images/icoEmail.png"
});
a.appendChild(img);

var li = document.querySelector("#someListItem");
li.appendChild(els);

The trouble with this though is that you have the same maintenance issues as before.

A better solution is to have the HTML code on the HTML page itself, and use scripting to update the appropriate parts of the HTML code.

<li>
  <a id="newWindow" href="frmenviarporcorreo.aspx">
    <img class="imgShare" src="images/icoEmail.png">
  </a>
</li>
var pData = {
  Pr_Id: 1234
};

var a = document.querySelector("#newWindow");
a.setAttribute("onclick", "window.open('" + a.href + "?pr_id=" + pData.Pr_Id + "', '_blank', 'location=yes,height=570,width=520,scrollbars=no,status=no')");
a.setAttribute("href", "#");

That way the HTML and the JavaScript are used for what they’re best at, with the JavaScript being used to adjust the behaviour of the HTML code.

Even better than this though is to use JavaScript to attach an onclick event to the link:

<li>
  <a id="newWindow" href="frmenviarporcorreo.aspx">
    <img class="imgShare" src="images/icoEmail.png">
  </a>
</li>
var pData = {
  Pr_Id: 1234
};

var a = document.querySelector("#newWindow");
a.addEventListener("click", function(evt) {
  evt.preventDefault();
  window.open(this.href + "?pr_id=" + pData.Pr_Id, '_blank',
    'location=yes,height=570,width=520,scrollbars=no,status=no');
});

This is the best of all the given solutions so far.

1 Like

That could be improved by wrapping it all in an IIFE to remove pData and a from the global namespace.

(function() {
var pData = {
  Pr_Id: 1234
};

var a = document.querySelector("#newWindow");
a.addEventListener("click", function(evt) {
  evt.preventDefault();
  window.open(this.href + "?pr_id=" + pData.Pr_Id, '_blank',
    'location=yes,height=570,width=520,scrollbars=no,status=no');
});
}());
1 Like

Yes indeed. We don’t know though how the rest of his code may be affected by such improvements, so further advice may have to be held off until the rest of it becomes known.

I understand it is a bad idea but right now it is like that. Your suggestion fixed my issue.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.