0

I know the title sounds a bit weird but so my problem is. I am developing a dynamic web project with eclipse/jsp. I am simply trying to bring a pop-up window by calling a javascript function and prevent redirect with a "return false". I will use servlet with ajax so I need to stop the execution with return false. The problem is javascript methods sometimes work, sometimes the method is not called at all. And sometimes the pop-up comes up yet return false does not effect and page is directed to "test.jsp?filePath=" I am keeping the javascript in a separate file called basic.js.

The JSP :

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script src="javascript/basic.js"></script>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    <form id="showFilePath">
         <input name="filePath" type="text">
         <input type="submit" onclick="shouldWork()">
    </form>
    <p id="output">
</body>
</html>

and the javascript method is simply :

function shouldWork() {
    alert("hello!");
    return false;
}

many thanks in advance

2 Answers 2

1

change

<input type="submit" onclick="shouldWork()">

to

<input type="submit" onclick="return shouldWork()">
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for the reply, in your case return false; doesn't work (the url changes). Actually it is quite unstable, different build with no changes yield to different results
0

Since you are using input type=submit, you can use "return false;" onSubmit event of form -

<form id="showFilePath" onSubmit="return false;">
             <input name="filePath" type="text">
             <input type="submit" onclick="shouldWork()">
        </form>

I would suggest you to use JQuery for ajax. Then use can use preventDefault() method to stop default behavior of form and call your own method -

<html>
<head>
<script>
        $(document).on("click", ".#mySubmit", function(e) {
            e.preventDefault();
            $.ajax({
                      url: formURL,
                      cache:'false',
                      type: "POST",
                      success:function(data, textStatus, jqXHR) {   
                        console.log("success");
                     }
                });
        });
</script>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    <form id="showFilePath">
         <input name="filePath" type="text">
         <input id="mySubmit" type="submit">
    </form>
    <p id="output">
</body>
</html>

1 Comment

thanks a lot Saurabh, the onsubmit command works yet the other problems prevail. i was going to use jquery for it yet first i had take care of invoking javascript from another file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.