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

时间:2015-08-28 12:26:06

标签: javascript html

我有一个网页,我显示一个包含数据库数据的表格。当我点击数据时我调用这个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) + "%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);
}

有任何想法如何解决这个问题?要么在没有window.event.returnvalue = false的情况下正确重定向javascript;或者当我有window.event.returnvalue = false时,如何解决javascript没有写入页眉/页脚的问题;有两个功能?

2 个答案:

答案 0 :(得分:0)

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

此外,不需要window.event.returnValue

答案 1 :(得分:0)

如果我更注重细节,这一切都可以避免......

当我将新行添加到我的javascript函数中时...我丢失了所有的javascript,因为该条目有错误...错误是我的愚蠢自我忘了在多行IF语句周围放置一个括号......一旦我这样做,我刚刚添加了window.event.returnvalue = false;一切都很好!