Unfortunatly, I need to forward file paths via ID. DIV ID="dir/dir2/file.txt" which contain illegal characters. Even base64 wont give solution. How to fix this?
-
What errors are you getting? What have you tried? – Richard JP Le Guen Nov 01 '11 at 22:17
-
the server simply erases those characters – user893856 Nov 01 '11 at 22:25
-
Ok... now, what server? Apache? IIS? What CMS? – Richard JP Le Guen Nov 01 '11 at 22:27
-
Most likely, it's easier to help you with eliminating the need for that kind of ID, than it is to help you encode the kind of ID you seem to be needing. If you explain some of the background that would help fix the underlying problem. – Jeroen Nov 01 '11 at 22:49
2 Answers
You fix this by not using illegal characters in the id
attribute. The attribute is meant for identification, not for passing data around. If you need to store element-related data within the DOM for JavaScript, you could for example use the data-*
attributes as explained in the HTML5 specification. Or alternatively just put them in a useful data structure within a script
block, so you can directly access it.

- 369,085
- 72
- 557
- 602
-
1@AshBurlaczenko Fair enough, though the attributes are supported anyway. I added an alternative without following HTML5. – poke Nov 01 '11 at 22:31
-
2@Ash - `data-*` attributes should work even in pre-HTML5 browsers, at least for the most common browsers. It certainly works in older IE, which is probably the mostly likely old browser still in use. – nnnnnn Nov 01 '11 at 22:36
-
@Ash: [Indeed they do](http://stackoverflow.com/questions/2412947/do-html5-custom-data-attributes-work-in-ie-6). HTML5 isn’t one thing, and was designed to be as backwards-compatible as possible. You can drop lots of bits of it into your existing pages without much worry. – Paul D. Waite Nov 01 '11 at 22:45
Helping you fix the underlying problem (causing the need for those IDs) is probably easier and better than helping you encode file paths in IDs. If you explain some background on the underlying problem perhaps a solution to that can be found?
Having said that, an answer to your question could be:
It can not be fixed, as you need characters that aren't allowed in IDs.
Or... think of some very absurd replace scheme to translate special path characters to valid ID characters. You could use the colon character in the ID as an escape char to indicate a unicode number is following. To give an idea of what I'm aiming at:
D:\directory something\wéírddir\file.txt
Would become something alike this ID:
D:2233directory:4455something:2233w:8877:0099rdir:2233file.txt
Where :2233
should be the actual number for a backslash, etc etc. Also, since you have to start with a letter in an ID (which is not necessarily the case in a path) you could just as a convention always start with a certain letter (and ignore that char when parsing the ID again server side).
Note: I do absolutely not recommend this solution (!!), but it seemed fun to figure out if it could be done at all :D
PS. Should also have a look at this SO question.