javascript语句执行的怪问题?
最近做一个在XML里使用javascript调用远程Web Services的程序,可是我遇到了一个怪问题:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="styles/common.xsl" type="text/xsl"?>
<html xmlns:u="http://housecall.trendmicro.com/xmlns/ui"
xmlns:locale="http://my.commons/XSL/PreProcess/Locale">
<head>
<u:header>
<locale:title>Trend Micro HouseCall 6.6 Example UI</locale:title>
</u:header>
<!-- Load the shared JavaScript code for the context pages -->
<script language="javascript" src="page.js"/>
<!-- Define the event listeners and start the system detection -->
<script language="javascript"><![CDATA[
window.handleKernelSelect = function(el) {
var show = el.value.indexOf("native") == -1;
var ctx = this.api.getContext();
// Set the current selection in the kernel
ctx.setOption("set-implementation", el.value);
// Show java alerts if necessary
if(!ctx.getBooleanOption("system-java-enabled"))
document.getElementById("no-java").style.display = show ? "block" : "none";
else if(!ctx.getBooleanOption("system-java-version-valid"))
document.getElementById("outdated-java").style.display = show ? "block" : "none";
}
window.initialize = function(api) {
if(this.UIInfo.getAttribute("boolean.cs.webcontent.skip.termsofuse") == "true" )
window.api.setOption("accept-terms-of-use", "true" );
// Set the initial values in the form
with(document.forms['kernelSelect']) {
elements['locale'].value =decodeUrlParameters()["locale"];
elements['accept-terms-of-use'].checked =
api.getContext().getBooleanOption("accept-terms-of-use");
}
// Load the terms of use
this.UIInfo.termsElement = document.forms['kernelSelect'].elements['terms-of-use'];
this.UIInfo.windowRef = this;
this.UIInfo.loadTermsOfUse(function(UIInfo, content, code) {
if(code == 200)
UIInfo.termsElement.value = content;
else if(code >= 400 && code <= 505)
UIInfo.termsElement.value = "FATAL: Failed with:" + code;
else
UIInfo.windowRef.loadTermsOfUse(); // retry
});
// Register Event Handlers
api.addEvent(window, "onsystemdetectionfinished", function(source) {
var el = document.forms['kernelSelect'].elements['implSelect'];
// Register the java alerts hide and show handler
for(var i = 0; i < el.length; i++) {
el[i].onclick = function() {
source.handleKernelSelect(this);
}
}
// Preselect the best option
var preSelImpl = source.selectedImplementation;
for(var i = 0; i < el.length; i++)
if(el[i].disabled == false && (
preSelImpl == null || el[i].value == preSelImpl
)) {
el[i].checked = true;
source.handleKernelSelect(el[i]);
break;
}
// Enable the start button
document.forms['kernelSelect'].elements['start'].disabled = false;
});
api.addEvent(window, "onimplementationdetected", function(source, implementation) {
var el = document.forms['kernelSelect'].elements['implSelect'];
el = ( implementation.indexOf("native") != -1 ? el[0] : el[1] );
el.value = implementation;
el.disabled = false;
});
api.addEvent(window, "onswitchingimplementation", function(source, implementation) {
// The javascript kernel does not need to switch the impl. we submit the form to switch it!
with(source.document.forms['kernelSelect']) {
elements["impl"].value = implementation;
onsubmit();
submit();
return false;
}
});
// Validate the input and sync the kernel options
document.forms['kernelSelect'].onsubmit = function() {
window_onload(3);
alert('确定吗?');
// Validate the license agreement
if(!this.elements['accept-terms-of-use'].checked) {
document.getElementById("terms-of-use-not-accepted").alert();
return false;
}
// Set the kernel options
window.api.setOption("accept-terms-of-use",
this.elements["accept-terms-of-use"].checked ? "true" : "false");
// Clear the terms of use field (to avoid submitting it)
this.elements["terms-of-use"].value = "*";
// Copy the selected kernel value to the hidden field
for(var i = 0; i < this.elements['implSelect'].length; i++)
if(this.elements['implSelect'][i].checked)
this.elements["impl"].value = this.elements['implSelect'][i].value;
}
// Bind the system detection
api.bindSystemDetection(window, document.getElementById("systemCheckContainer"));
// Get the previously selected implementation
this.selectedImplementation = api.getOption("set-implementation");
// Disable the automatic kernel select
//api.setOption("select-other-implementation", "true");
// Start the system detection
api.dispatchAction("test-availability");
}
function window_onload(work)
{
service.useService("http://192.1.1.203/webserver/Service1.asmx?WSDL","myselect");
str_province=service.myselect.callService("HelloWorld",work);
}
]]></script>
</head>
<body>
<div id="service" style="BEHAVIOR:url(http://localhost/Web/webservice.htc)"></div>
<form name="kernelSelect" method="GET" target="_top" action="native-frameset.xml">
<input type="hidden" name="locale"/>
<input type="hidden" name="impl"/>
<h1>
<locale:introduce-ks-h1>Scan module</locale:introduce-ks-h1>
</h1>
<u:box type="default">
<input type="radio" name="implSelect" id="r1" disabled="true"/>
<label for="r1">
<locale:introduce-ks-box1-plugin>Browser plugin</locale:introduce-ks-box1-plugin>
</label>
<br/>
<input type="radio" name="implSelect" id="r2" disabled="true"/>
<label for="r2">
<locale:introduce-ks-box1-java>Java? based scan module</locale:introduce-ks-box1-java>
</label>
<u:box id="no-java" type="error" style="display:none;">
<locale:introduce-ks-box1-no-java/>
</u:box>
<u:box id="oudated-java" type="error" style="display:none;">
<locale:introduce-ks-box1-outdated-java/>
</u:box>
</u:box>
<h1>
<locale:introduce-ks-h2>License Agreement</locale:introduce-ks-h2>
</h1>
<u:box type="gray">
<u:alert id="terms-of-use-not-accepted">
Please accept the "license agreement" in order to use Trend Micro HouseCall!
</u:alert>
<textarea rows="10" name="terms-of-use" readonly="readonly" style="width:636px;"/><br/>
<input type="checkbox" name="accept-terms-of-use" id="c1"/>
<label for="c1">
<locale:introduce-ks-box2-accept-license>
Yes, I accept the <a id="licenceAgreement">license agreement</a>
</locale:introduce-ks-box2-accept-license>
</label>
</u:box>
<p>
<button type="submit" name="start" disabled="true">
<locale:introduce-ks-start>
Start HouseCall
</locale:introduce-ks-start>
</button>
</p>
</form>
<u:footer>
<locale:copyright/>
</u:footer>
<!-- This is the container where the system detection code is dynamically added -->
<div id="systemCheckContainer"/>
</body>
</html>
以上那个onsubmit函数的第二行(alert('确定吗?');)注释掉的话,就无法插入数据库,按照上面的代码可以插入数据库可是我并不要一个警告框(alert('确定吗?');),从原理和html代码中的实践没有alert('确定吗?');都可以插入数据库。而且我把alert('确定吗?');改为return false;就可以插入数据库不过后面的代码就取消执行。应该没有return false;,调用的函数可以一直执行下去的啊。你们说这怪不怪?有没有这方面的高手指点一下。
[解决办法]
http://blog.csdn.net/chinmo/archive/2008/02/15/2096871.aspx
看看这个属性
[解决办法]
http://blog.csdn.net/chinmo/archive/2008/02/15/2096871.aspx
[解决办法]
1.alert()后才能执行...
原因可能是window_onload(3)里的回调函数还没有执行完毕,比如webservice的结果还没有返回。解决办法是加个标志,如果还没返回结果,延迟调用本函数(setTimeout,或看看我的博客中关于doLater的用法);
2、获得URL,可以看看document.location对象。它有:host,hostname,href,pathname,port,protocol等属性,应该可以满足你的需要。