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
Add a ment  | 

3 Answers 3

Reset to default 5

Give 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