问题
Here is my code. I am calling a JavaScript function in the code after insert query fires. But I get the error "return statement outside of function".
ClientScript.RegisterStartupScript(GetType(), "id", "return showPatientInfo('" + txtPatientID.Text + "');",true);
function showPatientInfo(pid) {
//alert(pid);
RedirectToPage("Create_Order.aspx", "pid=" + pid);
return false;
}
回答1:
I really wanted to rewrite your code as more idiomatic JavaScript. In the process, I found that formBypassQueryString variable was never getting assigned, which would probably cause problems. Here is my version:
function showPatientInfo(pid) {
redirectToPage("Create_Order.aspx", "pid=" + pid);
return false;
}
function redirectToPage(pageName, queryString) {
if (pageName === "" || queryString === "") return;
var keyValues = queryString.split("&"),
form = createQueryStringForm(pageName);
for (var i = 0; i < keyValues.length; i++) {
var keyValue = keyValues[i].split("="),
field = createQueryStringField(keyValue[0], keyValue[1]);
form.appendChild(field);
}
form.submit();
}
function createQueryStringField(name, value){
var field = document.createElement('input');
field.setAttribute('type', 'hidden')
field.setAttribute('name', name)
field.setAttribute('value', value)
return field;
}
function createQueryStringForm(pageName){
var form = document.createElement('form');
form.setAttribute('id', 'form-bypass-query-string');
form.setAttribute('method', 'POST');
form.setAttribute('action', pageName);
return form;
}
来源:https://stackoverflow.com/questions/10328084/javascript-error-return-statement-outside-of-function