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

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.