在Javascript中转义字符

时间:2016-03-29 16:26:59

标签: javascript escaping

function drawVisualization() {
    var query = new google.visualization.Query('http://spreadsheets.google.com/tq?key=XXXXXXX');
    query.setQuery('SELECT B, C, D, E, F, G, H where upper(B) like upper("%<?php echo $search; ?>%") or upper(D) like upper("%<?php echo $search; ?>%") or upper(E) like upper("%<?php echo $search; ?>%") or upper(F) like upper("%<?php echo $search; ?>%") order by G DESC label G "Data"');
    query.send(handleQueryResponse);
}

function handleQueryResponse(response) {
    if (response.isError()) {
        alert('Error in query: ' + response.getMessage() + ' ' + response.getDetailedMessage());
        return;
    }

    var data = response.getDataTable();

    var formatter = new google.visualization.PatternFormat(
        '<a href="{6}" target="_blank" onclick="var that=this;_gaq.push([\'_trackEvent\',\'Download archivio materiali\',\'{2}\',this.href]);setTimeout(function(){location.href=that.href;},200);return false;">{2}</a>');
    // Apply formatter and set the formatted value of the first column.
    formatter.format(data, [0, 1, 2, 3, 4, 5, 6], 2);

    var view = new google.visualization.DataView(data);
    view.setColumns([2, 0, 1, 4, 5]); // Create a view with the first column only.

    visualization = new google.visualization.Table(document.getElementById('table'));
    visualization.draw(view, {
        legend: 'bottom',
        allowHtml: true
    });

}

这是对此问题感兴趣的较小代码段:

var formatter = new google.visualization.PatternFormat('<a href="{6}" target="_blank" onclick="var that=this;_gaq.push([\'_trackEvent\',\'Download archivio materiali\',\'{2}\',this.href]);setTimeout(function(){location.href=that.href;},200);return false;">{2}</a>');

它确实工作正常(它输出一个具有正确数据的合法锚),除非在文档的标题中(由变量{2}输出)出现一个像双引号(“)的字符。锚语法会搞砸:

enter image description here

我是否需要使用像replace这样的函数来转义/替换双引号?我怎么能这样做?

JSFIDDLE

1 个答案:

答案 0 :(得分:0)

尝试更换{2}之类的内容:

var str = 'say: "blabla"';
str = str.replace(/"/g, '\\"');
console.log(str);

我已更新您的fiddle code并已插入:

// add a column with manipulated data
  var targetColIdx = data.getNumberOfColumns();          // will be the index of added column
  data.addColumn('string');
  var sourceColIdx = 2;                                  // get data from col 2
  var rowCount = data.getNumberOfRows();                 // save for loop condition
  var manipulated = "";
  for(var rowIndex=0; rowIndex<rowCount; rowIndex++)
  { manipulated = data.getValue(rowIndex, sourceColIdx); // get original
    manipulated = encodeURIComponent(manipulated);       // do some manipulation
    data.setCell(rowIndex, targetColIdx, manipulated);   // store in new column
  }

// modificated format: only use uri-encoded column in onclick attribute
  var formatter = new google.visualization.PatternFormat(
    '<a href="{6}" target="_blank" onclick="var that=this;_gaq.push([\'_trackEvent\',\'Download archivio materiali\',\'{'+targetColIdx+'}\',this.href]);setTimeout(function(){location.href=that.href;},200);return false;">{2}</a>');

我没有进一步调查点击事件处理程序中字符串会发生什么。也许您需要另外的编码或转义,如上面的RegExp示例中所示。您可以根据需要调整行manipulated = encodeURIComponent(manipulated);