admin管理员组文章数量:1307403
I have a javascript code that extracts filename as a variable from the current html file.
var filename=location.pathname.substring(location.pathname.lastIndexOf("/") + 1);
console.log(filename);
The filename for example is "new.html"
I need to append this variable to a folder path with href tag for opening another file file with that variable name in a new tab. I have tried concatenation but it doesn't work.
<a href="Foldername/'+filename+'" target="_blank">
Any help on this would be appreciated! Thank you!
Edit: I have posted the second part of my problem as another question on SO. Thank you for the help!
I have a javascript code that extracts filename as a variable from the current html file.
var filename=location.pathname.substring(location.pathname.lastIndexOf("/") + 1);
console.log(filename);
The filename for example is "new.html"
I need to append this variable to a folder path with href tag for opening another file file with that variable name in a new tab. I have tried concatenation but it doesn't work.
<a href="Foldername/'+filename+'" target="_blank">
Any help on this would be appreciated! Thank you!
Edit: I have posted the second part of my problem as another question on SO. Thank you for the help!
Share Improve this question edited Aug 14, 2017 at 7:32 user1954680 asked Aug 14, 2017 at 5:19 user1954680user1954680 231 silver badge5 bronze badges 5-
document.querySelector("a").href = "Foldername/" + filename;
– guest271314 Commented Aug 14, 2017 at 5:23 - SO isn't a code writing service; Please update your question with the code you've tried so far. – Soviut Commented Aug 14, 2017 at 5:23
-
You just want to create a string with the content of
<a href="Foldername/filename" target="_blank">
or an html element? – Szabolcs Commented Aug 14, 2017 at 5:23 - @Soviut OP has included the code that they have tried at Question – guest271314 Commented Aug 14, 2017 at 5:24
- You should have added both parts here – S4NDM4N Commented Aug 14, 2017 at 7:38
3 Answers
Reset to default 5Give an Id to your a tag
Html:
<a href="#" id="atag" target="_blank">
Javascript:
var filename=location.pathname.substring(location.pathname.lastIndexOf("/") + 1);
document.getElementById("atag").href= "Foldername/"+filename;
Try something like this:
var filename=location.pathname.substring(location.pathname.lastIndexOf("/") + 1);
var anchor = document.createElement('a');
anchor.href = `Foldername/${filename}`;
anchor.target = '_blank';
Then you can append your anchor
element wherever you want in your html.
If you are doing this using Javascript then do this way -
var filename=location.pathname.substring(location.pathname.lastIndexOf("/") + 1);
console.log(filename);
var html = "<a href='Foldername/'"+filename+" target='_blank'> "
el.innerHTML = html// el is the wrapper of anchor element.
If you want to keep the anchor tag in HTML code then -
Give a id/class to anchor - HTML -
<a href="" id="anchor" target="_blank">
JS -
var anchor = document.getElementById("anchor");
anchor.setAttribute("href", "Foldername/" + filename)
本文标签: jqueryAppend javascript variable to href folder pathStack Overflow
版权声明:本文标题:jquery - Append javascript variable to href folder path - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741774685a2396954.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论