Reputation: 19
Why do I get Unexpected end of input here:
row.
append($("<a href='javascript:downloadFile('" + filename + "');'>" + filename + "</a>"));
row
is defined, filename
is normal string?
Upvotes: 0
Views: 77
Reputation: 67505
The problem come from the four single-quotes '
you have inside append so it will be considered the href
attribute as :
'javascript:downloadFile('
Use double-quotes "
intead of single ones and escape them, example :
$('body').append($("<a href=\"javascript:downloadFile('" + filename + "');\">" + filename + "</a>"));
Hope this helps.
var filename = "file name test";
$('body').append($("<a href=\"javascript:downloadFile('" + filename + "');\">" + filename + "</a>"));
function downloadFile(file_name){
alert(file_name);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Upvotes: 0
Reputation: 664444
The error you are getting is from the use of the apostrophe as the delimiter for both HTML attribute and the JS string. A resulting source would look like
<a href='javascript:downloadFile('123.example');'>123.example</a>
<!-- ^ ^ ^ ^ -->
Instead of templating HTML without any escaping, use the DOM! jQuery is quite convenient to use here. For the JS string, use JSON.stringify
to escape it:
row.append($("<a />", {
href: 'javascript:downloadFile(' + JSON.stringify(filename) + ');',
text: filename
}));
But even better, don't use javascript:
URIs!!! Just add an event handler:
row.append($("<a />", {text: filename}).click(function(e) {
downloadFile(filename);
});
Upvotes: 0
Reputation: 3126
Try:
row.append($("<a href='javascript:downloadFile(\"" + filename + "\");'>" + filename + "</a>"));
Your original code results in the following string:
<a href='javascript:downloadFile('file1234.txt');'>file1234.txt</a>;
As you see, the second ' ends the string. So you href attribute is in fact:
href='javascript:downloadFile('
that won't work.
Upvotes: 0