-1

I have this code that will print a link, but where are this symbol "\" what can't print with JS.

document.getElementById('menu_links').innerHTML+="<li><a 
href='CRUD\data.php'>Data</a></li>";

2 Answers2

2

The '\' character is an escape character, it needs to be escaped to be printed like:

console.log('\\');

Will print '\'

Ryan Wilson
  • 10,223
  • 2
  • 21
  • 40
0

Just escape the character with another \

document.getElementById('menu_links').innerHTML+="<li><a href='CRUD\\data.php'>Data</a><\li>";
console.log('CRUD\\data.php'); //print path
<span id="menu_links"></span>

Edit: always use forward slash for URLs like http://example.com/CRUD/data.php, that way you wont have this problem and the code will be compatible across browsers.

Cheloide
  • 793
  • 6
  • 21
  • Good answer, but now the URL is using a backslash not a forward slash. – evolutionxbox Dec 05 '18 at 16:24
  • @evolutionbox is correct, Chrome automatically converts the backslash to a forward slash but that is not true for all browsers so it would be better to just correct it to a forward slash. – benvc Dec 05 '18 at 16:28