0

I have this javascript.

var url = http://localhost:8080/temp?a=b&c=2

I want to url to be

var url = http://localhost:8080/temp?a=b&c=2

i.e. without &

How can I do it?

Ashwin
  • 12,081
  • 22
  • 83
  • 117

2 Answers2

0

You can use a simple function to insert the string into an element as HTML, then grab the resulting text:

function deEntify(s) {
  var div = document.createElement('div');
  div.innerHTML = s;
  return div.textContent || div.innerText || '';
}

You may want to check that the input string doesn't include character sequences that can be misinterpreted.

You might also consider the accepted answer to Convert HTML Character Entities back to regular text using javascript, however there are about 250 entities if you want to account for all of them.

Community
  • 1
  • 1
RobG
  • 142,382
  • 31
  • 172
  • 209
0

The code

var url = http://localhost:8080/temp?a=b&c=2

is incorrect and does not work. You need to put the string literal in quotation marks. And you must write & as such, since it has no special meaning in JavaScript, only in HTML (and XML). So unless the value is to be parsed as HTML, use just

var url = 'http://localhost:8080/temp?a=b&c=2'
Jukka K. Korpela
  • 195,524
  • 37
  • 270
  • 390