最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - passing parameter to url via <a> tag int html - Stack Overflow

programmeradmin0浏览0评论

I have in a variable (var lan= urlParam('language')) the selected language. I want to pass this language as parameter (without using PHP) in a url in an "a" tag, like this:

<a href=".aspx?languageCode=lan"> 

but it doesn't work.

I am waiting for your answers. Thank you a lot.

I have in a variable (var lan= urlParam('language')) the selected language. I want to pass this language as parameter (without using PHP) in a url in an "a" tag, like this:

<a href="http://hotelsbined.sitewish.gr/HotelNameSearch.aspx?languageCode=lan"> 

but it doesn't work.

I am waiting for your answers. Thank you a lot.

Share Improve this question edited Sep 14, 2011 at 13:45 Quasdunk 15.2k3 gold badges39 silver badges47 bronze badges asked Sep 14, 2011 at 13:36 AlekaAleka 111 gold badge1 silver badge2 bronze badges 1
  • You want to dynamically, using JavaScript, add a new GET parameter to every link that is on the page. Is that correct? – Jan Hančič Commented Sep 14, 2011 at 13:38
Add a ment  | 

2 Answers 2

Reset to default 2

Your url is inside a string, so it won't put the value of your variable, but just the string "lan".

Give an ID to your link, so you can take it and change its url with javascript.

<a id="foo" href="http://hotelsbined.sitewish.gr/HotelNameSearch.aspx">

Then, with JS

document.getElementById('foo').setAttribute('href', 'http://hotelsbined.sitewish.gr/HotelNameSearch.aspx?languageCode=' + lan);

If I understand correctly, you can just use document.write to print the link and add the variable as the language parameter.

<script type="text/javascript">document.write("<a href=\"http://hotelsbined.sitewish.gr/HotelNameSearch.aspx?languageCode=" + lan + "\">link description</a>");</script>
发布评论

评论列表(0)

  1. 暂无评论