How to insert a javascript variable in <a href=""> [duplicate] - javascript

This question already has answers here:
Passing Javascript variable to <a href >
(8 answers)
Closed 3 years ago.
I have a javascript variable BASE_URL='http://localhost/BKTHP_WEB_NEW/';
Now I want to insert it inside a
something like <a href="BASE_URL+\view">

Use setAttribute to set the variable as a value for href
var BASE_URL='http://localhost/BKTHP_WEB_NEW/';
document.querySelector('a').setAttribute('href',BASE_URL)
<a href="">Link

You can use setAttribute on your a tag, and set the href dynamically
<a id="replace" href="">YOUR LINK</a>
const BASE_URL='http://localhost/BKTHP_WEB_NEW/';
document.querySelector('#replace').setAttribute('href', BASE_URL);

Related

How to make the links in Javascript code nofollow [duplicate]

This question already has answers here:
window.open() add a rel="nofollow" attribute
(3 answers)
Closed 5 years ago.
I want to make the links in this javascript code nofollow. Please help me how to do that.
<img src="https://example.png/>
According to https://developer.mozilla.org/en-US/docs/Web/API/Window/open you can add a third parameter in the window.open method that is a comma separated string with name value pairs - but rel is not supported...
Seems like html may be a good option.
<a href="www.example.com" rel="nofollow" target="_blank" >Link</a>
If you need to do this from js you could trigger a click on the link.

What is the purpose of this "javaScript:;"in hyperlink? [duplicate]

This question already has answers here:
What is href=javascript:;
(8 answers)
Closed 5 years ago.
What is the purpose of javascript:; in the href attributes of the hyperlinks?
<div data-trigger="spinner" id="spinner">
<span id="spinner-value"></span>
<input type="hidden" value="1" data-spin="spinner" data-rule="quantity" data-max="10">
-
+
</div>
the attribute href="javascript:;" is used to remove the behavior from the link.
If you would use eg. href="", the webpage would reload when you click the link. But with href="javascript:;" nothing will happen.
Later a script adds an event handler that will be executed when clicking this link.
EDIT: You need a or button elements as they are the semantic representatives for clickable objects.
To prevent links from refreshing webpage/redirecting you once clicked.
the purpose of "javascript:;" have save meaning with "javascript:void(0)"
Read here : javascript void functions

Get the value of <a> href using jQuery [duplicate]

This question already has answers here:
Get value of a custom attribute
(3 answers)
Closed 6 years ago.
I have this <a> href attribute
<a id="cancel" value="<?php echo $subID;?>" href="#">Cancel</a>
and I need to get its value ($subID) and assign it to a JavaScript variable. How can I do that?
Instead of using value, you should use data-value="<?php echo $subID;?>"
then you can get the value like this
var myVal = $('#cancel').data("value");
You could add the value as a custom data-attribute
HTML:
<a id="cancel" data-value="<?php echo $subID;?>" href="#">Cancel</a>
And then retrieve the value like this
JS:
$('#cancel').data('value');
You don't need jQuery. Plain JavaScript is easy enough:
Traditional:
var value = document.getElementById('cancel').getAttribute('value');
console.log(value);
<a id="cancel" value="Hello world!" href="#">Cancel</a>
Using a CSS selector:
var value = document.querySelector('#cancel').getAttribute('value');
Using jQuery
var value = $('#cancel').attr('value');
Although this works (at least in Chrome), a link doesn't have a value attribute so this HTML is invalid. If you use HTML5 (which you probably will), you can make it valid by naming the attribute 'data-value'. Attributes with the 'data-' prefix are allowed for this purpose.
There are other solutions too, but since they are out of scope here, please check Can I add custom attributes to HTML tags.
You have two ways (and more) to solve this
With jQuery:
var x= $('#cancel').val();
with JavaScript:
var x= document.getElementById("cancel").value;

Add URL to href tag with javascript [duplicate]

This question already has answers here:
How to change href of <a> tag on button click through javascript
(8 answers)
Closed 6 years ago.
How can I add a Url to the href tag with javascript? Can you help, please?
<a id="url-link" href="" onclick="" title="" class="btn">CLICK</a>
https://jsfiddle.net
Try this
var a = document.getElementById('url-link');
a.href="your link here";

jQuery: replace text inside link? [duplicate]

This question already has answers here:
change html text from link with jquery
(7 answers)
Closed 7 years ago.
I have the following html structure:
<div class="event tiny">
Something
</div>
And I want to replace the text of this link with "Anything" …
When doing this …
$('.event a').replaceWith("Anything");
… the text is replaced but also the link is gone.
When doing this … 
$('.event a').text().replaceWith("Anything");
… nothing happens.
Like this, if the new text is all the same.
$('.event a').text("Anything");
jQuery loops over the a elements, and replaces its content with the "Anything" text.
These suggestions, for setting the inner text & URL link within a a href didn't work for me (using JQuery v1.8.3)
Here's what did work though:
HTML
<a id="hrefAnalysisName" ></a>
JavaScript
$("a#hrefAnalysisName").attr("href", "/SomePage/ShowAnalysisDetails.aspx");
$("a#hrefAnalysisName").text("Show analysis");
Hope this helps!
You could also change the link by href if it's related to the URL.
$('a[href="/whatever"]').text("Anything");
You are able to get this result the next ways result will be the same:
HTML
<div class="event tiny">
<a class="html" href="/whatever">Something</a></br>
<a class="text" href="/whatever">Something</a></br>
<a class="replaceWith" href="/whatever">Something</a>
</div>
JS
$('.event a.html').html('Anything-html')
$('.event a.text').text('Anything-text')
$('.event a.replaceWith').replaceWith('<a class="replaceWith" href="/whatever">Anything-replaceWith</a>')

Categories