oconndav Posted August 6, 2007 Share Posted August 6, 2007 Hi,I'm a newbie to web design, and have been piecing together a site for a friend over the last few weeks basically by Googling everything I don't understand and trying to fit it all together. It's been an uplifting experience and have picked up some knowledge of MySQL, PHP, XML and AJAX along the way.I nearly have all of the functionality I need to move forward but there is one thing really annoying me because I can't fix it, and I have had much look searching through usual resources..Basically, I am dynamically displaying data on my HTML page by reading values from an XML file and writing to HTML using create.textnode().Everything is working fine except that one of the fields I want to display is a web address and I want this text field to display as a hyperlink on the webpage. So if the XML entry was <webaddress>http://www.hotmail.com</webaddress> I need that to display as a hyperlink to Hotmail on the page. Currently it just gets inserted as a text string.Example of the code I am using is: var webaddress = xmldoc.getElementsByTagName('webaddress').item(1); txt3 = document.createTextNode(webaddress); document.getElementById('webaddressdiv').appendChi ld(txt3); So in the above code I am reading the text value from the XML file, create a text node containing the text string of the webaddress and then writing it to a tag called webaddressdiv.Any ideas as to how I make this dynamic text string into a dynamic hyperlink ? Link to comment Share on other sites More sharing options...
jesh Posted August 6, 2007 Share Posted August 6, 2007 You can use a combination of the HTML DOM and the XML DOM to do this: var webaddress = xmldoc.getElementsByTagName('webaddress').item(1);var link = document.createElement("a");link.innerHTML = webaddress;link.href = webaddress;document.getElementById("webaddressdiv").appendChild(link); Link to comment Share on other sites More sharing options...
oconndav Posted August 9, 2007 Author Share Posted August 9, 2007 Thanks Jesh, that worked a treat!this maybe a dum question.. but how do I then force that link to open in a new browser window? Link to comment Share on other sites More sharing options...
jesh Posted August 9, 2007 Share Posted August 9, 2007 Check out the HTML DOM tutorial! http://www.w3schools.com/htmldom/dom_obj_anchor.aspOne way would be to set the target: link.target = "_blank"; Another way would be to attach an event handler to the click event: link.onclick = someClickHandlerThatOpensTheLinkInANewWindow; Link to comment Share on other sites More sharing options...
oconndav Posted August 9, 2007 Author Share Posted August 9, 2007 That worked (of course!)OK I'm off to read the tutorial now :-)Thanks for your help. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.