Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    Regular Coder
    Join Date
    Jun 2002
    Location
    Victoria, BC, Canada
    Posts
    962
    Thanks
    0
    Thanked 1 Time in 1 Post

    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.

  • #2
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,075
    Thanks
    0
    Thanked 256 Times in 252 Posts
    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
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #3
    Regular Coder
    Join Date
    Jun 2002
    Location
    Victoria, BC, Canada
    Posts
    962
    Thanks
    0
    Thanked 1 Time in 1 Post
    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>

  • #4
    Regular Coder
    Join Date
    Jun 2002
    Location
    Victoria, BC, Canada
    Posts
    962
    Thanks
    0
    Thanked 1 Time in 1 Post
    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?

  • #5
    Senior Coder
    Join Date
    Feb 2004
    Location
    Edinburgh
    Posts
    1,352
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #6
    Owl
    Owl is offline
    New Coder
    Join Date
    Jun 2002
    Posts
    62
    Thanks
    0
    Thanked 1 Time in 1 Post
    Hi Grant Palin,

    <script type="text/javascript">

    and follow glenngv suggestion.

    ( ) (• )
    >>V


  • #7
    Regular Coder
    Join Date
    Jun 2002
    Location
    Victoria, BC, Canada
    Posts
    962
    Thanks
    0
    Thanked 1 Time in 1 Post
    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.

  • #8
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,075
    Thanks
    0
    Thanked 256 Times in 252 Posts
    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
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #9
    Regular Coder
    Join Date
    Jun 2002
    Location
    Victoria, BC, Canada
    Posts
    962
    Thanks
    0
    Thanked 1 Time in 1 Post
    Sounds fine. And it's working, so I'm happy. Thanks!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •