我有一個網頁,我顯示一個表中充滿數據庫中的數據。我把這個javascript函數,當我點擊數據...爲了更改值...這重定向回我的炫魅,我在那裏處理它添加到數據庫中...document.location.href和window.event.returnvalue = false
function confirmCancel(id){
var d = document;
var bReply = confirm('Are you sure you want to cancel Order ' + id + '?');
if (bReply)
window.event.returnValue = false;
d.location.href = './main.aspx?action=cancel&orderid=' + id;
}
這將完全無能爲力鉻,直到我說window.event.returnValue =假....
我有兩個其他的功能,我想在使用它們...
function changePriority(id){
var strTemp;
var d = document;
strTemp = prompt('New Priority. Decrease the value to increase the priority','');
if (strTemp.length > 0 && strTemp.length <= 3 && !isNaN(strTemp))
d.location.href = './main.aspx?action=priority&orderid=' + id +'&priority=' + strTemp;
else
alert('Invalid Priority Value!');
}
和
function changeQuantity(id, item){
var strTemp;
strTemp = prompt('New Quantity.','');
if (strTemp.length > 0 && strTemp.length <= 3 && !isNaN(strTemp))
document.location.href = 'viewLoad.aspx?action=quantity&orderid=' + id +'&item=' + item + '&quantity=' + strTemp;
else
alert('Invalid Quantity Value!');
}
所以,現在,當我添加window.event.return = false;到任何其他功能...我失去了我的整個JavaScript ...所以我所有的頁眉/頁腳消失...你不必看這個代碼...它只是在那裏告訴你我是如何寫我的頭。 ..
function writeHeader (title){
var d = document;
d.write("<tr>");
d.write("<td width='35%' align='left' valign='bottom' rowspan='2' nowrap class='headerText'>");
d.write("<img src='./images/mie.jpg' border='0' class='headerLogo1'></img>");
d.write("</td>");
d.write("<td width='30%' align='center' valign='top' nowrap class='headerTitle'>");
d.write(title);
d.write("</td>");
d.write("<td width='35%' align='right' valign='center' id='timetext' nowrap class='headerText'>");
d.write(writeTime());
d.write("</td>");
d.write("</tr>");
d.write("<tr>");
d.write("<td align='center' valign='bottom' id='greetingtext' nowrap class='headerText'>");
d.write(writeGreeting());
d.write("</td>");
d.write("<td align='right' valign='bottom' id='datetext' nowrap class='headerText'>");
d.write(writeDate());
d.write("</td>");
d.write("</tr>");
d.write("<tr>");
d.write("<td width='100%' align='center' valign='center' colspan='3' class='headerText'>");
d.write("<hr color='gray'>");
d.write("</td>");
d.write("</tr>");
d.write("<tr height='35'>");
d.write("<td align='left' valign='top' colspan='1' class='headerText1'>");
d.write("<button name='btnPrint' style='cursor:hand;' class='headerText1' alt='Send this page to the printer' onMouseOver='this.style.color=\"orangered\";' onMouseOut='this.style.color=\"black\";' onClick='self.print();'>Print this Page</button>");
d.write("<button name='btnEmail' style='cursor:hand;' class='headerText1' alt='Email a link to this page' onMouseOver='this.style.color=\"orangered\";' onMouseOut='this.style.color=\"black\";' onClick='location.href=\"mailto:?subject=A Link from the Component Store Web Site&body=" + escape(location.href) + "%0\A%0\D\";'>Email this Page</button>");
d.write("</td>");
d.write("<td align='center' valign='top' colspan='2' class='headerText'>");
d.write("<table width='100%' align='center' cellspacing='0' cellpadding='0' border='0'>");
d.write("<tr>");
d.write("<td width='95%' align='center' valign='center' class=''></td>");
d.write("<td align='center' valign='bottom' style='cursor:hand;' class='fontSize1' onMouseOver='this.style.color=\"orangered\";' onMouseOut='this.style.color=\"black\";' onClick='changeStyleSheet(\"smaller\")'>A</td>");
d.write("<td align='center' valign='bottom' style='cursor:hand;' class='fontSize2' onMouseOver='this.style.color=\"orangered\";' onMouseOut='this.style.color=\"black\";' onClick='changeStyleSheet(\"default\")'>A</td>");
d.write("<td align='center' valign='bottom' style='cursor:hand;' class='fontSize3' onMouseOver='this.style.color=\"orangered\";' onMouseOut='this.style.color=\"black\";' onClick='changeStyleSheet(\"larger\");'>A</td>");
d.write("</tr>");
d.write("</table>");
d.write("</td>");
d.write("</tr>");
// Get the routine started that will update the date/time.
setInterval('updateDateTime()', 1000);
}
任何想法如何解決這個問題?要麼正確的JavaScript重定向沒有window.event.returnvalue = false;或者當我有window.event.returnvalue = false時,如何解決javascript不寫頁眉/頁腳的問題?在兩個功能?
我不知道你的問題的答案,但我可以說你**需要重構這段代碼。 [document.write](http://stackoverflow.com/questions/802854/why-is-document-write-considered-a-bad-practice),[setInterval eval](http://stackoverflow.com/questions/ 6081560 /是否曾經是一個很好的理由傳遞一個字符串到settimeout),[returnValue](http://stackoverflow.com/questions/20045162/event-returnvalue-is-deprecated -please-use-the-standard-event-preventdefault)都是差編碼實踐的例子。並且所有'.write's *不容易調試 – CodingIntrigue
RGraham所說的+注意,如果這個響應不會被取消,所有提供服務器響應的動作都會阻止對'location.href'的更改。這些操作例如點擊鏈接並提交表單。 – Teemu
謝謝@Teemu我會檢查任何在 –