How to pass variable in href in html

I am trying to give another variable when href=""

Hello

Above is my code.

I want to pass value apple named fruit variable when going to a.html. How can I do this in JavaScript or jQuery?

asked Dec 13, 2018 at 8:03

How to pass variable in href in html

임지웅임지웅

1211 gold badge1 silver badge8 bronze badges

3

The only way that JS can communicate (easily) between URLs is by creating a parameter on your link. For example:

Hello

On the receiving page, fetch that parameter data. If you are wanting the data embedded into the link purely via JS, you can append to the HREF using something similar to this by using the jQuery library:

$("a").attr("href", '?fruit=' + 'apple');

answered Dec 13, 2018 at 8:09

How to pass variable in href in html

Just do this on your main page:

Hello

And on a.html, add this code:

function getParameterByName(name, url) {
        if (!url) url = window.location.href;
        name = name.replace(/[\[\]]/g, '\\$&');
        var regex = new RegExp('[?&]' + name + '(=([^&#]*)|&|#|$)'),
            results = regex.exec(url);
        if (!results) return null;
        if (!results[2]) return '';
        return decodeURIComponent(results[2].replace(/\+/g, ' '));
    }

    var fruit = getParameterByName('fruit');
Hello

answered Dec 13, 2018 at 8:08

Jack BashfordJack Bashford

41.9k11 gold badges46 silver badges76 bronze badges

4

You can reset the href value by concatenating the existing value with the new value:

var el = document.getElementById('a');
el.href += '?fruit=apple';
console.log(el.href);
Hello

answered Dec 13, 2018 at 8:07

How to pass variable in href in html

Giulio BambiniGiulio Bambini

4,5554 gold badges19 silver badges34 bronze badges

2

As i understand you want to add query parameter fruit=apple to anchor tag url when new page opened. So you need to use window.open() to open new page

document.getElementById("a").onclick = function(e){
  e.preventDefault();
  window.open(this.href+"?fruit=apple", '_blank');
}
Hello

Because new tab doesn't work in snippet, check it in jsfiddle.

answered Dec 13, 2018 at 8:12

MohammadMohammad

20.5k15 gold badges53 silver badges80 bronze badges

1

Would would need a way to acceess Html parameters. I dont believe its possible in html. There is a good load of Javascript Methods however.

For example:

function getUrlVars() { 
var vars = {}; 
var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) { 
   vars[key] = value; 
});
return vars; 

}

You would call this function like so

var fruit = getUrlVars()["fruit"];
console.log(fruit);
//Apple

Your URL in href would look like:

a.html?fruit=Apple

However this is hardcoded, if you wanted some nice dynamics with this prehpas use angularJS to create this with.

Source

answered Dec 13, 2018 at 8:16

How to pass variable in href in html

1

href”, append the variable to it (Here we have used a variable named “XYZ”). Then we need to append the value to the URL. Now our URL is ready with the variable and its value appended to it. In the example below, we will append a variable named 'XYZ' and its value is 55.

Can we pass value in href?

The HTML Anchor Link is assigned an OnClick event handler and when clicked, the value of the TextBox is set in the HREF attribute of the HTML Anchor Link using JavaScript. When the HTML Anchor Link is clicked, the TextBox value is passed to the other Page as QueryString parameter.
In order to send the parameters in the URL you donot need to enclose the value inside quote ('') or doublequotes ("") . You just need to send the values (or the multiple values) as shown below.. Also keep in mind that you need to keep track of the urlencoding.

How do you pass a variable to a tag in HTML?

You can accomplish this elegantly with Printing Scriptlets, assuming your email HTML body is in a separate file within your project. To fill out the template, use the HtmlService to get the template first, then treat the variables inside the printing scriptlets as object properties of the template.