Part of my code:
<p id="demo">{$value.file_name}</p>
<script type="text/javascript">
var str = document.getElementById("demo").innerHTML;
var res = str.replace("/var/www/html/biology/demo", "");
document.getElementById('para').innerHTML = res;
</script>
<a href="#para" id='para'>Download</a>
This part of the url will already be present: "a.b.c.d.edu/bio/cluster/"
$value.file_name contains "/var/www/html/biology/demo/files/mpijobs/107/mothership/data/job107_0_0_output.tif"
After the script, "para" contains the edited path which is "/files/mpijobs/107/mothership/data/job107_0_0_output.tif" (the removal of "/var/www/html/biology/demo")
The code:
Download
provides a clickable link to "a.b.c.d.edu/bio/cluster//var/www/html/biology/demo/files/mpijobs/107/mothership/data/job107_0_0_output.tif"
and what I want to do is replace "{$value.file_name}" inside the brackets with "para" (and what it represents) so that the download link is linked to
"a.b.c.d.edu/bio/cluster//files/mpijobs/107/mothership/data/job107_0_0_output.tif"
Sorry, I misunderstood.
If the a href attribute is set like so:
Download
You can use in the javascript:
str.setAttribute("href", res);
EDIT:
Ok I got it. Sorry about this strenuous exercise. Here's what you should write:
<p id="demo">{$value.file_name}</p>
<a href="#para" id='para'>Download</a>
<script type="text/javascript">
var str = document.getElementById("demo").innerHTML;
var res = str.replace("/var/www/html/biology/demo", "");
para = document.getElementById('para');
para.href = res;
</script>
Related
How can I display a href link inside an if condition using javascript?
<!DOCTYPE html>
<html>
<body>
<script>
function myFunction() {
var x = document.getElementById("fname");
if(x.value.indexOf("aaaa")>=0)document.getElementById("result").innerHTML= ""+ x.value + "" ;
else document.getElementById("result").innerHTML=x.value;
}
</script>
<form method="post" action="esegui.php">
Name<br><input type="text" id="fname" onkeyup="myFunction()">
</form>
<p id="result"></p>
</body>
</html>
You can create anchor tag using JavaScript following way:
var aTag = document.createElement('a');
aTag.setAttribute('href',"http://www.google.com");
aTag.innerHTML = "aaa";
document.body.appendChild(aTag);
Put above code in if condition will create anchor tag(link).
Although your question is a little bit confusing, I'll try to help you
If what you want is to create a specific link, do this:
var link = window.document.createElement("a");
var str = window.document.getElementById("theidofyourinput").value;
link.textContent = str;
link.href = 'http://www.linkofyourpage';
document.getElementsByTagName('body')[0].appendChild(link);
Try researching the createElement() method in JavaScript, calling it to create an anchor element in the body of your if statement e.g.
var myLink = document.createElement('a');
myLink.appendChild(document.createTextNode('link name')); //visible text
myLink.setAttribute('href', 'http://google.com'); //link href attribute
document.getElementById('someId').appendChild(myLink); //append the element
There are many ways to do this but you have not mentioned specific scenario so do like this:
In HTML:
<div id="test"></div>
In Java script:
var markup = "link";
var v = document.getElementById("test") ;
v.innerHtml(v );
I have the following JS function to pull out all words that start with “#” and replace the css styling of them. I would like to add a url that redirects to a query of all items containing the tag.
How would I save the stripped tags, and then dynamically add it to the url for each one?
$('.find-hashtag').each(function(index) {
var str = $(this).html();
var edt = str.replace(/(^|\s)(#[A-Za-z]+)/ig, "$1<a href='{% url "hashtagged_item_list” *stored_value_goes_here* %}'>$2</a>");
$(this).html(edt);
});
Thank you in advance for any help!
What I'm hoping to accomplish:
<p class="find-hashtag">This is sample #html text.</p>
$('.find-hashtag').each(function(index) {
var str = $(this).html();
var tag = "html";
var edt = str.replace(/(^|\s)(#[A-Za-z]+)/ig, "$1<a href='{% url "hashtagged_item_list” tag %}'>$2</a>");
$(this).html(edt);
});
First, store the url generated by django with a dummy tag. Because django is operating server side and js is client side, they cannot be combined:
var url = '{% url "hashtagged_item_list" "dummy" %}';
Then,extract the tag and replace 'dummy' with it:
var edt = str.replace(/\b#([A-Za-z]+)/ig, function(match, tag) {
return '#' + tag + '');
});
I have a page with a download button like this:
<a href="http://www.example.nl/filename.pdf" download>DOWNLOAD</a>
Below, I want (text) to automatically display "filename.pdf" (rather than having to do this by hand hundreds of times).
I found the script below that displays the filename of the PAGE but I want it to display the FILENAME of a HREF I've used on the actual page.
Any help is much appreciated.
<script type="text/javascript">
var segment_str = window.location.pathname;
var segment_array = segment_str.split( '/' );
var last_segment = segment_array.pop();
document.write(last_segment);
</script>
Thanks in advance!
Not sure where you want the "text" to display... so I put it in a div
<a href="http://www.example.nl/filename.pdf" download>DOWNLOAD</a>
<div id="result">
</div>
The big change, is to get all the "a" tags, using getElementsByTagName... and then iterating over the list, and then you can use the string split, and pop off the last segment before appending it to a destination.
var input = document.getElementsByTagName('a');
for(i = 0;i < input.length; i++)
{
var segment_str = input[i].href;
var segment_array = segment_str.split( '/' );
var last_segment = segment_array.pop();
document.getElementById("result").innerText += last_segment;
}
Maybe this will help.
<div id=download1></div>
<script>
var filename = 'example.pdf';
$('#download1').html('' + filename + '');
</script>
I use:
<script type="text/javascript">
document.addEventListener("DOMContentLoaded", function(event) {
var field1 = document.getElementById("field_wy4dm0");
field1.addEventListener("change", combineFields);
function combineFields() {
var val1 = field1.value;
var val1re = val1.match("/(.*)?")[1];
var str = document.getElementById("changeThisMovie").innerHTML;
var res = str.replace("yeC3AisTs2Y", val1re);
document.getElementById("changeThisMovie").innerHTML = res;
}
});
</script>
It works great to get a youtube video id which is located between "/" and "?" in the "val1" var and replace the old video id "yeC3AisTs2Y" that is located inside a div with the id="changeThisMovie" with the new one ("val1re").
The problem is it adds "?" in the end of the new video id so instead of getting:
/videoid?feature=embed...
I get:
/videoid??feature=embed...
How do i fix this?
Thanks!!!
I think you want to escape the ? In your regex to mean the literal ? symbol - regex will interpret as ignore previous block
So you could try as follows:
var val1re = val1.match("/(.*)\?")[1];
I am trying to make a button which would encode my link into ascii and than on click redirect me to that new encoded address.
I already have the encoding part but the button just displays the converted code on click. I would like it instead to redirect me to that encoded page.
<body>
<p>Click the button to encode a URI.</p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
<script>
function myFunction() {
var uri = "https://webpage.com/submit.do?firstName=Ždžrgr&lastName=Žrđredj&phone=(24324) 09942&email=email#email.net”;
var res = encodeURI(uri);
document.getElementById("demo").innerHTML = res;
}
</script>
</body>
Can someone please help me with this? I am new to JavaScript.
You can use location.href to redirect the page. getElementById() gets the element with id inside the paranthesis from the page for getting or setting the current value.
function myFunction() {
var uri = "https://webpage.com/submit.do?firstName=Ždžrgr&lastName=Žrđredj&phone=(24324) 09942&email=email#email.net";
var res = encodeURI(uri);
location.href = res;
}
You're setting the innerHTML instead of changing the location. Try using
this.document.location = res;
instead.