I've attached onsubmit handler in a form tag like this:
<form action="file.php" method="post" onsubmit=" return get_prepared_build(this);" >
but whatever global variable (defined earlier, of course) I try to change inside get_prepared_build() function - later it apprears non-modified. Looks like that this function deals with a local copy of everything, even document property values are not saved.
Are there scope/visibility problems when javascript functions are called this way from tags/attributes?
Here is the function:
function give_link(results)
{
document.title = 'visibility test';
return false;
}
then below in the document I have
<script>alert('test' + document.title );</script>
As a result - in the window I have a new title, but the alert box displays old variable value.
To answer your last question, no, there are no scope/visibility problems when JavaScript functions are called from tags/attributes:
<script type="text/javascript">
var x = 'Hello';
function get_prepared_build(f) {
alert('Start get_prepared_build: x=' + x + '; document.cookie=' + document.cookie);
x = 'There';
// deliberately invalid cookie for test purposes only
document.cookie = 'test';
alert('End get_prepared_build: x=' + x + '; document.cookie=' + document.cookie);
return false;
}
</script>
<form action="file.php" method="post" onsubmit="var ret=get_prepared_build(this);alert('Outside get_prepared_build: x=' + x + '; document.cookie=' + document.cookie);return ret;">
<input type="submit">
</form>
As was mentioned in comments, a sample of code demonstrating your specific problem would be helpful.
EDIT: In your example, the function that updates document.title
is never invoked, or is invoked after you alert()
the current value, so document.title
doesn't appear to change.
<script type="text/javascript">
function changeDocumentTitle(f) {
// this only runs onsubmit, so any alert()s at the bottom
// of the page will show the original value before the
// onsubmit handler fires
document.title = 'new value';
return false;
}
</script>
<form onsubmit="return changeDocumentTitle(this);">
<input type="submit">
</form>
<script type="text/javascript">
// this executes when the page loads, so it will show
// the value before any onsubmit events on the page fire
alert(document.title); // original value
</script>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.