Reputation: 4370
var url = "C:\xampp\htdocs\wcf2\wcf/attachments/5d/18-5dffacdfe6d2cf1db8dfabbb5b53ae8dc65bd325";
results in Uncaught SyntaxError: Unexpected token ILLEGAL
. Is there any way to fix it in javascript or do I need to escape it before in PHP?
Edit: My script looks like this:
<script>
window.onload = function() {
thingiurlbase = "{@$__wcf->getPath('td')}js";
thingiview = new Thingiview("viewer");
thingiview.setObjectColor('#C0D8F0');
thingiview.initScene();
var url = "C:\\xampp\htdocs\\wcf2\\wcf/attachments/5d/18-5dffacdfe6d2cf1db8dfabbb5b53ae8dc65bd325";
thingiview.loadSTL(url);
thingiview.setRotation(false);
}
</script>
Upvotes: 1
Views: 920
Reputation: 13054
You need to escape backslashes.
var url = "C:\\xampp\\htdocs\\wcf2\\wcf/attachments/5d/18-5dffacdfe6d2cf1db8dfabbb5b53ae8dc65bd325";
If the string is printed from PHP, you can use addslashes to do the work for you.
$test = 'C:\xampp\htdocs\wcf2\wcf/attachments/5d/18-...';
echo $test . "\n";
echo addslashes($test);
>> C:\xampp\htdocs\wcf2\wcf/attachments/5d/18-...
>> C:\\xampp\\htdocs\\wcf2\\wcf/attachments/5d/18-...
By the way, two things are incorrect with your approach:
The slashes in your string should all point the same way. If you want it to be used as a URL, use forward slashes e.g. http://stackoverflow.com/posts/17775495
If you want to give the browser a URL that it can use to access some resource on your server, this URL cannot be a file path (such as C:\xampp\htdocs\...
.) Instead, it has to be either relative to your site root, or an absolute URL (such as http://dropbox.com/my/file.pdf
). Don't forget that a web user can only access files on your server through the web server.
Upvotes: 1
Reputation: 234795
You need to escape the backslash characters.
var url = "C:\\xampp\\htdocs\\wcf2\\wcf/attachments/5d/18-5dffacdfe6d2cf1db8dfabbb5b53ae8dc65bd325";
Otherwise, JavaScript tries to interpret \x
, \h
, and \w
as escaped special characters.
In this particular case, though, it looks like you could just replace the \
with /
instead:
var url = "C:/xampp/htdocs/wcf2/wcf/attachments/5d/18-5dffacdfe6d2cf1db8dfabbb5b53ae8dc65bd325";
Upvotes: 2