Forum Moderators: open

Message Too Old, No Replies

Page Title Into A Form Question

         

Jon_King

2:11 pm on Nov 8, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I think I need to do this with Javascript. I'm trying to fetch the page title and put it into this form value:

<input type="hidden" name="pagetitle" value="RETRIEVE PAGE TITLE HERE" />

I'm very green in javascript, can someone give me clue how to accomplish this?

Scally_Ally

4:15 pm on Nov 8, 2005 (gmt 0)

10+ Year Member



<input type="hidden" name="pagetitle" value="<SCRIPT LANGUAGE="JavaScript">
<!--
document.write(document.title);
//-->
</SCRIPT>" />

BlobFisk

4:21 pm on Nov 8, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Unfortunately it is invalid to write scripts like that as an attribute value! You could try:


<script type="text/javascript">
document.getElementById('inputID').value = document.title;
</script>

with


<input type="text" id="inputID" />

HTH

Jon_King

4:58 pm on Nov 8, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Neither worked. Man I feel thick headed, getting the title into a var is ok but getting it assigned to 'value' is not.

I guess there is no way to get the 'value' attribute assigned anything other than hardcoded text?

BlobFisk

5:18 pm on Nov 8, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That's strange Jon - I've tested what I posted and it works fine! Could you post your relevant code snippets?

whoisgregg

5:32 pm on Nov 8, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The <input> has to come before the script portion in your page's source code.

Fotiman

5:40 pm on Nov 8, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Why not this:

<script type="text/javascript">
document.write("<input type='hidden' name='pagetitle' value='" + document.title + "'>");
</script>

If you're writing XHTML, just remember to include the closing slash:

<script type="text/javascript">
document.write("<input type='hidden' name='pagetitle' value='" + document.title + "' />");
</script>

Jon_King

6:52 pm on Nov 8, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes Fotiman, That did work.

FYI,
Exactly what I was trying to do was pass Y!Q (Yahoo's context search) the page title for context. This is in leu of enclosing part of the page text within the Value attribute.

Due to the content on this site, the page titles give far better results and I simply wanted a way to code a var as opposed to hard coding.... works great now.

Thanks all.