Popo
Popo

Reputation: 2460

Create anchor href with javascript and not have host url in link

I am attempting to create a link dynamically using javascript, but the host url is prepended to the link every time. How do I stop this from happening. I created an example on js fiddle. the link created prepends "http://fiddle.jshell.net/mTgtr/show/" to the www.google.com link.

http://jsfiddle.net/mTgtr/

var eventLink = "www.google.com";
var elEventBlock = document.createElement('div');
var elA = document.createElement('a');
elA.setAttribute('href', eventLink);
elA.setAttribute('target', '_blank');
elA.innerHTML = "test";
document.getElementById("myDiv").appendChild(elA);

Upvotes: 1

Views: 1808

Answers (1)

Grant Thomas
Grant Thomas

Reputation: 45083

You should prefix the URL with the protocol, such as http://, https://, ftp://, etc.

Upvotes: 5

Related Questions