Question

Is it possible to call a JS function, with a value from an ASP script?

I thought it was possible, so I tried the following method but I can't seem to get it to run. As you can see, I have an ASP variable "totalPages" in the JS call.

Response.Write "<form name=""pageSkip"" onsubmit=""return validatePageSkip("&totalPages&")"">"

I then have the JS function, which is:

function validatePageSkip(totalPages)
{
     if (document.pageSkip.pn.value.length > totalPages)
     {
         alert("Please enter a page number within range!");
         document.pageSkip.pn.focus();
         return(false);
     }
     document.pageSkip.submit();
}
  1. Is this possible?
  2. If yes, what am I doing wrong?

UPDATE

It won't even work just using this, when I submit with an empty text box:

Response.Write "<form name=""pageSkip"" id=""pageSkip"" onsubmit=""return validatePageSkip()"" method=""post"" action=""?cpr="&pageRange&"#main"">"
Response.Write "<input type=""text"" name=""cpn"" id=""cpn"" spellcheck=""false"" autocomplete=""off"" size=""3"" class=""pageSkipBox"" value="""&currentPage&""">"
Response.Write "</form>"

With this simple JS function:

function validatePageSkip()
{
if (document.pageSkip.cpn.value.length < 1)
{
alert("Test!");
document.pageSkip.cpn.focus();
return(false);
}
document.pageSkip.submit();
}

I really don't know what is wrong. The form submits perfectly but it just won't call the function.

Can you spot anything?

Était-ce utile?

La solution

Yes it is possible. but you need to make sure you escape your quotes correctly

Response.Write "<form name=""pageSkip"" onsubmit=""return validatePageSkip("&totalPages&")"">"

Personally I would prefer doing something like this in the head

<head>
   <script type="text\javascript">
     var totalpages = <%=totalPages%>;
   </script>
</head>

Then

Response.Write "<form name=""pageSkip"" onsubmit=""return validatePageSkip(totalpages)"">"
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top