最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Passing string variable in alert box - Stack Overflow

programmeradmin3浏览0评论

I have below script where im passing scriptlet variable to javascript function. But there is no alert box displaying.. Please let me what im missing here..

<script language="JavaScript">
window.onload = function(){
    <%
      String res = request.getParameter("Message");
    %>
    var Value = "<%=res%>";
    alert(Value);
    document.Form.submit();
};
</script>

Thanks in advance

I have below script where im passing scriptlet variable to javascript function. But there is no alert box displaying.. Please let me what im missing here..

<script language="JavaScript">
window.onload = function(){
    <%
      String res = request.getParameter("Message");
    %>
    var Value = "<%=res%>";
    alert(Value);
    document.Form.submit();
};
</script>

Thanks in advance

Share Improve this question edited Oct 8, 2013 at 5:16 John asked Oct 8, 2013 at 5:15 JohnJohn 2,14113 gold badges36 silver badges45 bronze badges 4
  • remove the double quotes if you want the script to run and print res inside of the value variable – Jay Harris Commented Oct 8, 2013 at 5:19
  • Not a solution ! but for script tag language is an obsolete attribute; try using type="text/javascript" instead of language="JavaScript" – Vishal Commented Oct 8, 2013 at 5:20
  • Thanks for the additional info, @Vishal – John Commented Oct 8, 2013 at 5:29
  • All, its not displaying the alert box – John Commented Oct 8, 2013 at 6:08
Add a ment  | 

3 Answers 3

Reset to default 1

try this....

<script type="text/javascript">
window.onload = function(){
<%
  String res = request.getParameter("Message");
%>
var Value = <%=res%>;
windows.alert(Value);
document.Form.submit();
};
</script>

What you need to use is <%= ... %>. Try using this:

<script type="text/javascript">
window.onload = function(){
<%
  String res = request.getParameter("Message");
%>
var Value = <%=res%>;
alert(Value);
document.Form.submit();
};
</script>

@John Why you creating scriptlet tag inside javascript. Instead try using this,

<%
String res = request.getParameter("Message");
%>
<script type="text/javaScript">
    window.onload = function(){

    var Value = "<%=res%>";
    alert(Value);
    document.Form.submit();
};
</script>

else try using jquery plugin for your purpose,

<%
    String res = request.getParameter("Message");
%>
<script type="text/javaScript">
   $(document).ready(function() {  
      var Value = "<%=res%>";
      alert(Value);
      $('form#myForm').submit();
   });
</script>
发布评论

评论列表(0)

  1. 暂无评论