0

I have a JS

function check(obj) {

for( i=0; i<obj.elements.length; i++){
    if(obj.elements[i].type=="checkbox"&&obj.elements[i].checked){
        if(confirm(onSubmitMessage)){
            return true;
        }
        else{
            return false;
        }
    }
}
alert(alertMessage);
return false;
}

It's called from jsp page like this:

<script src="/TestAppChanged/check.js" type="text/javascript">
    var onSubmitMessage = '"<bean:message key="body.onsubmit.delete"/>"';
    var alertMessage = '"<bean:message key="body.alert.delete"/>"';
</script>
...
<form action="MyAction" method="POST"
onsubmit="return check(this)">

The problem is in that it doesn't see these glabal variables: onSubmitMessage and alertMessage. I thought that the problem was in the way that these things are set and changed it's values to usual strings like "qwe" but it didn't work again. So script in it's body simply doesn't see these variables. And the question is how to get them from script?

6 Answers 6

3

The <script> tag is used either to load an external file, like <script src="path/to/file.js"></script> or to define JS code inside the tag, like:

<script type="text/javascript">
    // Your JS code.
</script>

You can't have both on the same tag. Thus, split your code:

<script src="/TestAppChanged/check.js" type="text/javascript"></script>
<script type="text/javascript">
    var onSubmitMessage = '"<bean:message key="body.onsubmit.delete"/>"';
    var alertMessage = '"<bean:message key="body.alert.delete"/>"';
</script>
Sign up to request clarification or add additional context in comments.

1 Comment

I think you meant "You can't have both on the same tag"... ;)
2

A block between <script src=""></script> parsed when the browser don't support javascript. You need another <script> block, and put in this your variables.

Comments

1

It doesn't see the globar variabes because you declare a source in the script tag. You should remove the global variables and declare them before the code of the function, in the js page, and then simply include the page like this:

<script src="/TestAppChanged/check.js" type="text/javascript"></script>

Comments

1

You should have two script tags. One for the variables. And then another one below it, linking to your javascript file.

Comments

1

You can set contents in tag OR set the src attribute. It should be:

<script type="text/javascript">
    var onSubmitMessage = '"<bean:message key="body.onsubmit.delete"/>"';
    var alertMessage = '"<bean:message key="body.alert.delete"/>"';
</script>
<script src="/TestAppChanged/check.js" type="text/javascript"></script>

Comments

1

Don't mix the src attribute and code within the tag. Use two tags.

Comments

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.