document.location.href and window.event.returnvalue = false



我有一个网页,我从数据库显示一个表的数据。我调用这个javascript函数当我点击数据…为了改变值…这将它重定向到我的主页,在那里我处理将它添加到数据库…

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;
}

这将完全不做chrome,直到我添加window.event.returnValue = false....

我还有两个函数,我想在…

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) + "%0A%0D";'>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正确重定向没有窗口。事件。returnvalue = false;或者如何解决javascript不写页眉/页脚的问题,当我有window.event.returnvalue = false;在两个函数中?

document.location是一个只读属性,不应该用于重定向。相反,您应该使用window.location。(https://developer.mozilla.org/en-US/docs/Web/API/Document/location)

不需要window.event.returnValue

如果我多注意一些细节,这整件事本来是可以避免的。

当我添加新的行到我的javascript函数…我失去了我所有的javascript,因为有一个错误与该条目…错误是我愚蠢的自己忘记在多行IF语句周围放一个括号…这样做之后,我只需要添加window。event。returnvalue = false;一切都很好!

最新更新