...

View Full Version : Outputting a button from ASP through Javascript does not work



Grant Palin
06-09-2004, 11:17 PM
In my ASP page, I want to output some buttons depending on the current page. I can create them through ASP fine, and originally just dropped them on the page. However, since the buttons use javascript in their onclick event, the buttons are useless in non-javascript browsers.

I then went on to create text-link versions of the buttons, using <a href=...> instead of buttons. I then output those links through ASP, inside a noscript tag. My idea was to output the buttons using javascript, and if that didn't work, then the plain text links would be visible and usable instead.

The plain text links appear and are usable when I disable javascript in my browser. However, when I re-enable javascript, the buttons do not appear, and neither do the text links (which is to be expected).

So, I have the following ASP code that creates strings holding the HTML to create the buttons and the links:


'Add First and Previous buttons'
objButtonString.Add(" <input type=""button"" value=""First"" onclick=""document.location.href=" & _
"'results.asp?" & objQueryString.Value & "&currentpage=1';"" />")
objLinkString.Add(" | <a href=""results.asp?" & objQueryString.Value & "&currentpage=1"">First</a> |" & vbCrLf)
objButtonString.Add(" <input type=""button"" value=""Previous"" onclick=""document.location.href=" & _
"'results.asp?" & objQueryString.Value & "&currentpage=" & intCurPage - 1 & "';"" />")
objLinkString.Add(" | <a href=""results.asp?" & objQueryString.Value & "&currentpage=" & intCurPage - 1 & """>Previous</a> |" & vbCrLf)

'Add Next and Last buttons'
objButtonString.Add(" <input type=""button"" value=""Next"" onclick=""document.location.href=" & _
"'results.asp?" & objQueryString.Value & "&currentpage=" & intCurPage + 1 & "';"" />")
objLinkString.Add(" | <a href=""results.asp?" & objQueryString.Value & "&currentpage=" & intCurpage + 1 & """>Next</a> |" & vbCrLf)
objButtonString.Add(" <input type=""button"" value=""Last"" onclick=""document.location.href=" & _
"'results.asp?" & objQueryString.Value & "&currentpage=" & intPageCount & "';"" />")
objLinkString.Add(" | <a href=""results.asp?" & objQueryString.Value & "&currentpage=" & intPageCount & """>Last</a> |" & vbCrLf)


And the following code outputs those string:


objNavString.Add(" <script type=""type/javascript"">" & vbCrLf)
objNavString.Add(" <!--" & vbCrLf)
objNavString.Add(" document.write('" & objButtonString.Value & "')" & vbCrLf)
objNavString.Add(" -->" & vbCrLf)
objNavString.Add(" </script>" & vbCrLf)
objNavString.Add(" <noscript>" & vbCrLf)
objNavString.Add(objLinkString.Value)
objNavString.Add(" </noscript>" & vbCrLf)


All well and good. I load the page, no errors...and no buttons (and yes, javascript is enabled).

Here's the HTML I got from viewing the source:


<script type="type/javascript">
<!--
document.write(' <input type="button" value="First" onclick="document.location.href='results.asp?type=LHA&subtype=&region=1&region=2&region=3&region=4&region=5&year=1976&year=1981&year=1986&year=1987&year=1988&year=1989&agegroup=totals&gender=a&output=browser&rowsperpage=25&currentpage=1';" /> <input type="button" value="Previous" onclick="document.location.href='results.asp?type=LHA&subtype=&region=1&region=2&region=3&region=4&region=5&year=1976&year=1981&year=1986&year=1987&year=1988&year=1989&agegroup=totals&gender=a&output=browser&rowsperpage=25&currentpage=1';" /> <input type="button" value="Next" onclick="document.location.href='results.asp?type=LHA&subtype=&region=1&region=2&region=3&region=4&region=5&year=1976&year=1981&year=1986&year=1987&year=1988&year=1989&agegroup=totals&gender=a&output=browser&rowsperpage=25&currentpage=3';" /> <input type="button" value="Last" onclick="document.location.href='results.asp?type=LHA&subtype=&region=1&region=2&region=3&region=4&region=5&year=1976&year=1981&year=1986&year=1987&year=1988&year=1989&agegroup=totals&gender=a&output=browser&rowsperpage=25&currentpage=4';" />')
-->
</script>
<noscript>
| <a href="results.asp?type=LHA&subtype=&region=1&region=2&region=3&region=4&region=5&year=1976&year=1981&year=1986&year=1987&year=1988&year=1989&agegroup=totals&gender=a&output=browser&rowsperpage=25&currentpage=1">First</a> |
| <a href="results.asp?type=LHA&subtype=&region=1&region=2&region=3&region=4&region=5&year=1976&year=1981&year=1986&year=1987&year=1988&year=1989&agegroup=totals&gender=a&output=browser&rowsperpage=25&currentpage=1">Previous</a> |
| <a href="results.asp?type=LHA&subtype=&region=1&region=2&region=3&region=4&region=5&year=1976&year=1981&year=1986&year=1987&year=1988&year=1989&agegroup=totals&gender=a&output=browser&rowsperpage=25&currentpage=3">Next</a> |
| <a href="results.asp?type=LHA&subtype=&region=1&region=2&region=3&region=4&region=5&year=1976&year=1981&year=1986&year=1987&year=1988&year=1989&agegroup=totals&gender=a&output=browser&rowsperpage=25&currentpage=4">Last</a> |
</noscript>


Now, I'm thinking it's got something to do with the quotes in the resulting HTML when doing document.write in javascript...am I right? I'm not too familiar with javascript, so I turn to those who are to point out the (quite likely simple) error.

glenngv
06-10-2004, 05:34 AM
You must be encountering js errors because of the single quotes.

document.write(' <input type="button" value="First" onclick="document.location.href='results.asp?type=... />')

What you should do is escape the single quotes in the html string by putting backslash before them.

objButtonString.Add(" <input type=""button"" value=""First"" onclick=""document.location.href=" & _
"\'results.asp?" & objQueryString.Value & "&currentpage=1\';"" />")

Grant Palin
06-10-2004, 05:51 PM
Thanks glennv, I tried that. No visible errors still, but still not buttons. The slashes I added to the string, before the single quotes, actually show up in the HTML source:


<script type="type/javascript">
<!--
document.write(' <input type="button" value="Next" onclick="document.location.href=\'results.asp?type=LHA&subtype=&region=1&region=2&region=3&region=4&region=5&region=6&region=7&region=9&region=10&region=11&region=12&region=13&region=14&region=15&year=1981&year=1986&agegroup=totals&gender=t&rowsperpage=25&output=browser&currentpage=2\';" /> <input type="button" value="Last" onclick="document.location.href=\'results.asp?type=LHA&subtype=&region=1&region=2&region=3&region=4&region=5&region=6&region=7&region=9&region=10&region=11&region=12&region=13&region=14&region=15&year=1981&year=1986&agegroup=totals&gender=t&rowsperpage=25&output=browser&currentpage=2\';" />')
-->
</script>

Grant Palin
06-10-2004, 06:03 PM
Can I even run that small script in the middle of the page? Do scripts have to be located in the header of the HTML file?

jbot
06-10-2004, 06:05 PM
Can I even run that small script in the middle of the page? Do scripts have to be located in the header of the HTML file?

you can put them wherever you like, so long as they are included within script tags :thumbsup:

Owl
06-10-2004, 08:36 PM
Hi Grant Palin,

<script type="text/javascript">

and follow glenngv suggestion.

( •) (• )
>>V

Grant Palin
06-10-2004, 08:47 PM
<script type="text/javascript">


Duh! I knew it had to be something simple! :rolleyes: :mad: :o

Thanks for that tip, Owl.

Byt he way, the slashes I added to escape the single quotes inside the string, they appear int he HTML source. Is that to be expectred? I get no errors...And the buttons appear and work fine.

glenngv
06-11-2004, 03:50 AM
The escaped single quotes are needed in the javascript code.
It is because you use single quotes (blue) as the string delimiter and you need to add the single quotes (red) in the string.


document.write('<input ... onclick="document.location.href=\'results.asp?type=...&currentpage=2\';" /> <input ... onclick="document.location.href=\'results.asp?type=...&currentpage=2\';" />')

Grant Palin
06-11-2004, 04:53 AM
Sounds fine. And it's working, so I'm happy. Thanks!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum