JavaScript error “Return statement outside of function”

独自空忆成欢 提交于 2021-02-08 11:45:32

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!