Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 06-09-2004, 10:17 PM   PM User | #1
Grant Palin
Regular Coder

 
Join Date: Jun 2002
Location: Victoria, BC, Canada
Posts: 962
Thanks: 0
Thanked 1 Time in 1 Post
Grant Palin is an unknown quantity at this point
Outputting a button from ASP through Javascript does not work

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:
PHP Code:
'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 "';"" />")
objLinkString.Add("      | <a href=""results.asp?" objQueryString.Value "&currentpage=" intCurPage """>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 "';"" />")
objLinkString.Add("      | <a href=""results.asp?" objQueryString.Value "&currentpage=" intCurpage """>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:
PHP Code:
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:
Code:
<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.
Grant Palin is offline   Reply With Quote
Old 06-10-2004, 04:34 AM   PM User | #2
glenngv
Supreme Master coder!


 
glenngv's Avatar
 
Join Date: Jun 2002
Location: Los Angeles, CA Original Location: Philippines
Posts: 10,241
Thanks: 0
Thanked 112 Times in 111 Posts
glenngv will become famous soon enough
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\';"" />")
__________________
Glenn
_____________________________________________
Play Tower of Hanoi Android app (Ad-FREE!)
Play Tower of Hanoi Android app (FREE!)
Go to Tower of Hanoi Leaderboard
Play Tower of Hanoi Facebook app
glenngv is offline   Reply With Quote
Old 06-10-2004, 04:51 PM   PM User | #3
Grant Palin
Regular Coder

 
Join Date: Jun 2002
Location: Victoria, BC, Canada
Posts: 962
Thanks: 0
Thanked 1 Time in 1 Post
Grant Palin is an unknown quantity at this point
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:
Code:
<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 is offline   Reply With Quote
Old 06-10-2004, 05:03 PM   PM User | #4
Grant Palin
Regular Coder

 
Join Date: Jun 2002
Location: Victoria, BC, Canada
Posts: 962
Thanks: 0
Thanked 1 Time in 1 Post
Grant Palin is an unknown quantity at this point
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?
Grant Palin is offline   Reply With Quote
Old 06-10-2004, 05:05 PM   PM User | #5
jbot
Senior Coder

 
Join Date: Feb 2004
Location: Edinburgh
Posts: 1,352
Thanks: 0
Thanked 0 Times in 0 Posts
jbot is an unknown quantity at this point
Quote:
Originally Posted by Grant Palin
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
jbot is offline   Reply With Quote
Old 06-10-2004, 07:36 PM   PM User | #6
Owl
New Coder

 
Join Date: Jun 2002
Posts: 62
Thanks: 0
Thanked 1 Time in 1 Post
Owl is an unknown quantity at this point
Hi Grant Palin,

<script type="text/javascript">

and follow glenngv suggestion.

( ) (• )
>>V

Owl is offline   Reply With Quote
Old 06-10-2004, 07:47 PM   PM User | #7
Grant Palin
Regular Coder

 
Join Date: Jun 2002
Location: Victoria, BC, Canada
Posts: 962
Thanks: 0
Thanked 1 Time in 1 Post
Grant Palin is an unknown quantity at this point
Quote:
Originally Posted by Owl
<script type="text/javascript">
Duh! I knew it had to be something simple!

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.
Grant Palin is offline   Reply With Quote
Old 06-11-2004, 02:50 AM   PM User | #8
glenngv
Supreme Master coder!


 
glenngv's Avatar
 
Join Date: Jun 2002
Location: Los Angeles, CA Original Location: Philippines
Posts: 10,241
Thanks: 0
Thanked 112 Times in 111 Posts
glenngv will become famous soon enough
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.

Code:
document.write('<input ... onclick="document.location.href=\'results.asp?type=...&currentpage=2\';" />    <input ... onclick="document.location.href=\'results.asp?type=...&currentpage=2\';" />')
__________________
Glenn
_____________________________________________
Play Tower of Hanoi Android app (Ad-FREE!)
Play Tower of Hanoi Android app (FREE!)
Go to Tower of Hanoi Leaderboard
Play Tower of Hanoi Facebook app
glenngv is offline   Reply With Quote
Old 06-11-2004, 03:53 AM   PM User | #9
Grant Palin
Regular Coder

 
Join Date: Jun 2002
Location: Victoria, BC, Canada
Posts: 962
Thanks: 0
Thanked 1 Time in 1 Post
Grant Palin is an unknown quantity at this point
Sounds fine. And it's working, so I'm happy. Thanks!
Grant Palin is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:52 PM.


Advertisement
Log in to turn off these ads.