homepage Welcome to WebmasterWorld Guest from 54.204.94.228
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

    
JS created URL appaears as text
from hidden form field value
RedTCat




msg:3174255
 11:45 pm on Nov 30, 2006 (gmt 0)

I have created a form that send it's contents to a different page as as "Latest News" type page. I want the option to include a clickable URL link on the resulting page. I have a hidden form field named "link", and a bit of JS to add the "a href" tag to the URL field (urltext) of the form...

The resulting text appears in the page as a fully formed HTML, not the clickable link I wanted.

I'm not really experienced with JS (OK you noticed) ... but can't work out how to make the resulting text real HTML in the code, so the link is clickable on the page.

Here is the JS (it is part of a bigger bit of JS that also checks the fields have input)

>>>>>

if (theForm.urltext.value == "")
theForm.link.value='';
else
theForm.link.value='<a target=\"_blank\" href=\"'+ theForm.urltext.value +'\">;'+ theForm.urltext.value +'</a>';

<<<<<

The result on the page typically looks like:

<a target="_blank" href="www.test.com">www.test.com</a>

========================

(OK, I use FrontPage - so shoot me, but in my experience most people who deride FP have not used it, whereas I have used FP and Dreamweaver. DreamWeaver was a nightmare)

 

Fotiman




msg:3174926
 4:01 pm on Dec 1, 2006 (gmt 0)

I'm not sure how exactly your form is setup, so I'll just show you a generic example:

<div id="foo"></div>
<script type="text/javascript">
// Get the target for a new link
var container = document.getElementById("foo");
// Create a new link element
var aLink = document.createElement("a");
aLink.href = "http://www.example.com";
aLink.appendChild( document.createTextNode("Example") );
// Append the link to the container
container.appendChild(aLink);
</script>

RedTCat




msg:3174964
 4:37 pm on Dec 1, 2006 (gmt 0)

Thanks Fotiman, but I'm not sure if you didn't understand what I wanted - or if I don't undertand your reply.

The "urltext" is the input of a form field... I want to pass this input, reformatted as a URL in HTML to the results page.

So, I type www.example.com in the form field (called urltext) and an HTML <a herf= ...> tag is sent as hidden field called "link", so that the results page has a clickable link on it ... all I get is the complete <a herf= ...> tag!

If you did understand, I can't follow the logic, or the JS, sorry :-(

Fotiman




msg:3175199
 7:34 pm on Dec 1, 2006 (gmt 0)


So, I type www.example.com in the form field (called urltext) and an HTML <a herf= ...> tag is sent as hidden field called "link", so that the results page has a clickable link on it ... all I get is the complete <a herf= ...> tag!

Ok, so the form is submitted and the form processing page gets the values something like this:

urltext=www.example.com
link=<a href="...">

Something like that?
1. Why don't you just use a server side language like ASP or PHP to spit out the link?
2. If you still want to use JavaScript to create the link, then use my example, but replace the "Example" text with that of the link that was passed in:

<div id="foo"></div>
<script type="text/javascript">
// Get the target for a new link
var container = document.getElementById("foo");
// Create a new link element
var aLink = document.createElement("a");
aLink.href = "http://www.example.com"; // WHATEVER WAS PASSED IN LINK
aLink.appendChild( document.createTextNode("Example") ); // WHATEVER WAS PASSED IN URLTEXT
// Append the link to the container
container.appendChild(aLink);
</script>

RedTCat




msg:3175464
 11:19 pm on Dec 1, 2006 (gmt 0)

I'm not using ASP or PHP, because I can't write those either (and either would probably involve messing with related pages).

I now realise you don't understand what I want.

The only data sent forward is the entire html code for a link.

The JS is on the form page that send data to a results page.

My script example does more-or-less work, the correct information is gathered and passed on to the results page, but it does not look like a link on the results page, just "visible" HTML code...

"urltext" is the name of the form field collecting the URL information.

"link" is the name of the hidden field created to apss the URL as formed HTML.

I'm just trying to make it easy for a totally non-pc orientated person to type a URL as part of a form, and for it to appear as a link.

[edited by: RedTCat at 11:25 pm (utc) on Dec. 1, 2006]

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved