/    Sign up×
Community /Pin to ProfileBookmark

Using javascript to pass url to a html link

How can I pass the current url as an html link using javascript. I tried

<a href=”javascript:document.location.href;”><img src=”https://xxxx/image.png” border=”0″></a>

The image shows with html link. Upon clicking on it, the page only print the url.

I am a newbie, need help on the exact syntax. thank you.

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@jedaisoulMar 15.2015 — You do not need to use JavaScript in the first place, use:

<a href="#">
Copy linkTweet thisAlerts:
@browser23authorMar 15.2015 — What I would like to achieve is to have the href in <a href=test.link.com>test </a> taken

from the browser url.

the html souce code will look like <a href=test.link.com?extra_char>test </a>....and this will trigger a proper working code.

I am working a custom code that I can't edit its internal process so I am limited to what I can do. The vendor said the page will support javascript.

Originally I use php $_SERVER['QUERY_STRING']; to capture the url and echo this to the href part of <a></a> but since they don't support this anymore I am forced to find an alternative way of passing the url into the href link.
Copy linkTweet thisAlerts:
@browser23authorMar 15.2015 — From google, I tried

<a id="pageLink">test</a>

<script>

var link = document.getElementById("pageLink");

link.setAttribute("href", window.location.href);

</script>

When I land to this page, with url https://test.com?extrachar

The souce code will show the javascript I put... <a id="pageLink">test</a> ..what I need to have <a href=https://test.com?extrachar>test </a> and this should work.

I guess if I can use javascript to replace an href.. I would like a code

<a href=https://test.com>test</a>.. then use javascript to replace https://test.com from the url of the page, since this url now looks like https://test.com?exctrachar.

resulting page source code should be <a href=https://test.com?extracchar>test</a>
Copy linkTweet thisAlerts:
@rootMar 15.2015 — Your objective being ...
Copy linkTweet thisAlerts:
@Troy_IIIMar 15.2015 — How can I pass the current url as an html link using javascript. I tried

<a href="javascript:document.location.href;"><img src="https://xxxx/image.png" border="0"></a>

The image shows with html link. Upon clicking on it, the page only print the url.

I am a newbie, need help on the exact syntax. thank you.[/QUOTE]


<a href="javascript:location=location">

<img src="image.png" border="0">

</a>
×

Success!

Help @browser23 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.15,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...