Jump to content

[SOLVED] HTML tags and hyperlinks in JavaScript?


Petty_Crim

Recommended Posts

I know theres probably an easy solution to this but nothing seems to come up on google.

 

Basically I'm using javascript to create a url, which is then assigned to a variable and outputted to the web page. The problem is though its outputting it as a string rather then a proper url.

 

My code

text_url='www.google.com';
url="<a href='" + text_url + "'>Click Here</a>";

cell_url = row.insertCell(-1);			
cell_url.appendChild(document.createTextNode(url);

 

This will output as a string:

<a href="www.google.com">Click Here</a>

 

I need it outputted as a url though ie Click Here

you need to use createElement

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
<title>Untitled</title>
</head>

<body>
<script language="JavaScript" type="text/javascript">
<!--
var html_element, body_element, a_element, text_node;

// the document element in an XHTML document
// is always the html element
html_element = document.documentElement;
// the body element is the second and last child
// of the html element
body_element = html_element.lastChild;


a_element = document.createElement("a");
a_element.href = 'http:\/\/www.google.com';
//newA.text = 'go to google';
text_node = document.createTextNode("A Link to Google");
a_element.appendChild(text_node);
body_element.appendChild(a_element);
// -->
</script>


</body>
</html>

Thanks BenInBlack I was just about to solve this one myself, I found this on the net but yours seems pretty similar:

 

link = document.createElement ('A');
link.appendChild (document.createTextNode ('Click here'));
link.setAttribute ('href', 'google.com');

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.